hjk41的日志

Avatar

Effective C++ 45: Know what C++ silently writes and calls

C++ will automatically generate constructors, destructors, copy constructors address-of operator and assignment operator for your class if you don't declare them.

The default constructor does nothing except for enabling you to create an object of that class. However, if you have declared any constructor for that class, C++ will not generate a default constructor. Of course, the default constructor of the base class will be invoked automatically.

The default destructor does nothing, either. The default destructor is nonvirtual except when the base class have a virtual destructor.

The copy constructor and assignment operator copy the non static members of the class, and calls the copy constructor / assignment operator of the base class. However, if we write copy constructors ourselves, what will be automatically invoked is the default constructor of the base class. As for the assignment operator, no member function of the base class will be automatically called.

If you want to disallow the above functions, you can just declare them private and leave them undefined.

评论已关闭