"Effective C + +" study notes (iii)

Source: Internet
Author: User

Original articles, reproduced please specify the source: http://blog.csdn.net/sfh366958228/article/details/38816913


Chat from Beidaihe Tourism return, the win their hearts continue to work, next will continue to study "effective C + +", today is the construction/destruction/assignment operation part.
Article 05: Understand C + + silently write and invoke those functions when the compiler is processed, there is no absolute empty class, for example:
Class empty{};
The compiler declares a default constructor for it, a copy constructor, a copy assignment operator, and a destructor:
Class Empty{public:empty () {...}; Empty (const empty &RHS) {...}; ~empty () {...}; empty& operator= (const Empty &RHS) {...};}
Attention!!! Just being declared, they are only created by the compiler when these functions are actually called. In general, compiler output destructors are non-virtual, but if the base class of the class itself declares a virtual destructor, it is another matter. If there is any constructor in the class, the compiler no longer creates a default constructor for it.
Summary:The compiler can secretly create a default constructor, a copy function, a copy assignment operator, and a destructor for class.

Article 06: If you do not want to use the compiler automatically generated functions, you should explicitly deny that there are no two identical leaves in the world, so when we declare a leaf object and want to copy out another leaf through this leaf object, this behavior is illegal.
Leaf L1; Leaf L2; Leaf L3 (L1); Attempts to copy L1 should not be compiled L3 = L2; Attempts to copy L2 should not be compiled
For this function to be implemented, to prevent the compiler from automatically generating, we can define the copy constructor and the copy assignment operator as private. But this is not absolute security, because the member function and the friend function can still call it, of course, still the laws. If we only declare them and do not define them, then if someone calls them they will get a connection error.
Class Leaf{public:...private: ... Leaf (const leaf&); leaf& operator= (const leaf&);}
Of course, so we cross the compile period and the connection period, want to make the connection period error early to compile time is also possible.
Class Uncopyable{protected:uncopyable () {}//Allow derived object construction and destruction ~uncopyable () {}private:uncopyable (const uncopyable&); /But block copyinguncopyable& operator= (const uncopyable&);} Class Leaf:private uncopyable{...}
The implementation and use of uncopyable is rather subtle, including the need not to inherit it with public, and the uncopyable destructor is not necessarily virtual, etc., but this will likely result in multiple inheritance, and multiple inheritance sometimes prevents the white space base class from being optimized (empty Base class optimization). Of course, so far, the content is only stopped before c++11 release, c++11 after the release to reject the compiler's "good intentions" to provide a new way:
Class Leaf{public:...private: ... Leaf (const leaf&) = delete; leaf& operator= (const leaf&) = delete;}

Summary:1) In order to dismiss the function provided by the compiler automatically, the corresponding member function can be declared private and not implemented. Using a base class like uncopyable is also a practice. 2) A member function can also be defined as a delete on a compiler that supports c++11.
Article 07: Declaring a virtual destructor for a polymorphic base class when an object is defined in the heap and it has a base class with a non-virtual destructor, the part of the derived class (derived class) is not destroyed when the object is deleted. The phenomenon of local destruction will be taken as a result. It is not difficult to know from the keywords in the previous paragraph that the solution is to give the base class A virtual destructor, the purpose of which is to allow the derived class implementation to be customized. Any class with the virtual function is almost certain that it should also have a virtual destructor. If Claa does not contain a virtual function, it usually means that it will not be used as a base class. When class is not a base class, it is a bad idea to make a destructor for virtual. The virtual function can be polymorphic, deciding when to invoke which virtual function, because it carries a special message that is pointed out by a so-called vptr (virtual table pointer). Vptr points to an array of function pointers, called VTBL (virtual table), each with the virtual function
Summary:1) A polymorphic base class (base class) with polymorphic properties should declare a virtual destructor. If class has any virtual functions, he should have a virtual destructor. 2) The purpose of classes is not to declare a virtual destructor if it is not used as a base class, or if it is not for polymorphism.

"Effective C + +" study notes (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.