Remote default designer headache

My C ++ book talks about this (lippman, C ++ primer, 5th ed., P. 508):

A synthesized default constructor is defined as remote if the class ... has a const member whose type does not explicitly define the default constructor and which does not have an initializer in the class. (empirical mine)

Why then does this code cause an error?

class Foo { Foo() { } }; class Bar { private: const Foo foo; }; int main() { Bar f; //error: call to implicitly-deleted default constructor of 'Bar' return 0; } 

The above rule indicates that this should not be an error, because Foo explicitly defines a default constructor. Any ideas?

+4
source share
3 answers

To fix your mistake. You need to make Foo :: Foo () public.

 class Foo { public: Foo() { } }; 

Otherwise, I believe that it is personal.

Is this what you are looking for?

+7
source

The default constructor is omitted when building a class is not trivial.

This means that either there is an explicit constructor that takes parameters (and then you cannot assume that it can be constructed without these parameters)

Or, if one of the members or base classes must be initiated in the construct (they themselves do not have a trivial constructor)

+4
source

I think this should work

 class Foo { public: Foo() { } }; class Bar { public: Bar() : foo() {} private: const Foo foo; }; 
+1
source

All Articles