0

Why B is implicitly deleted?

Although this code has many errors, those are for the reasons but one of them state that B::B() is implicitly deleted. What does that mean how B is deleted. https://code.sololearn.com/ca0a10a3a165

27th Mar 2021, 7:16 AM
Abhishek Dimri
Abhishek Dimri - avatar
3 Respostas
+ 3
You need to put constructor of class A in public block not in private block. You also need to define `B b;` in main function, your code doesn't have main function. Function `disp()` needs a return type specification, and there's a missing semicolon after you print <marks>. Later on someone may answer your question theoretically, I'm no good in theoretical explanation.
27th Mar 2021, 8:39 AM
Ipang
+ 2
Adding to what Ipang stated, if you don't provide a constructor for your class, the compiler implicitly generates a default constructor for you, which default-initializes all members. B() = default; In a derived class, this also involves calling the default constructor of the base class. However, since A::A() is private, B::B() has no access to the base class constructor, which prevents the compiler from generating a correct default constructor. Hence, it is implicitly deleted: B() = deleted; If the compiler attempted to generate it anyway, the program would immediately fail to compile, as the constructor would be ill-formed. This way, the program only crashes if you try to instantiate B.
27th Mar 2021, 12:45 PM
Shadow
Shadow - avatar
+ 1
I'm glad you came along Shadow I couldn't explain why it is happening, just figured some steps to fix it based on compiler error messages.
27th Mar 2021, 1:15 PM
Ipang