I was reading this: https://en.cppreference.com/w/cpp/language/rule_of_three
And my understanding from this is that, if you want to have a base class with a virtual destructor, the you need to define all 5 special functions (extracted from the rule of 0 section):
class base_of_five_defaults
{
public:
base_of_five_defaults(const base_of_five_defaults&) = default;
base_of_five_defaults(base_of_five_defaults&&) = default;
base_of_five_defaults& operator=(const base_of_five_defaults&) = default;
base_of_five_defaults& operator=(base_of_five_defaults&&) = default;
virtual ~base_of_five_defaults() = default;
};
however when I try to instaiate that class I get the error saying that "default c'tor is not created":
base_of_five_defaults b; // Error
Then if I generate the default its ok:
base_of_five_defaults() = default;
But I did not understand that this was needed at all... so I am confused why its not there. I thought the only reason the compiler does not generate a default constructor is if you specify a non-default constructor...
If you DO need to specify the default c'tor then the class in the example is un-constructable - which seems odd.
Here is the link to my full live example: https://godbolt.org/z/qPvjd6r51
From https://en.cppreference.com/w/cpp/language/default_constructor:
Implicitly-declared default constructor
If no user-declared constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare a default constructor as an inline public member of its class.
I guess this means if base_of_five_defaults(const base_of_five_defaults&) = default;
is declared then it is considered user-declared even though its "default"?