Effective C + + essay (2)

Source: Internet
Author: User

Article 5 Understanding what functions C + + silently writes and calls

The compiler automatically generates copy constructors, copy assignment operators, destructors, constructors, which are both public and inline, where inline means that their definitions are in the header file.

Assuming there is a data member of a reference type, the assignment above is not correct because the reference cannot be changed

clause 6 If you do not want to use a function generated automatically by the compiler, you should explicitly deny

The assignment constructor above, the assignment operator can be generated automatically by the compiler, in order to deny the use of the above two functions, you can create a class that defines a private member of this function, so that these functions are not used

Class Uncopyable {protected:uncopyable () {}~uncopyable () {}private:uncopyable (const uncopyable&); uncopyable& operator= (const uncopyable&);} Class Myclass:private uncopyable{};

Article 7 declaring a virtual destructor for a polymorphic base class

It should be declared as virtual by the destructor, so that the resources of each class level can be reconstructed cleanly at the multi-state call to prevent the memory leakage and so on.

It is convenient to declare pure virtual destructors in some classes. Pure virtual functions produce abstract classes-classes that cannot be instantiated (that is, objects of this type cannot be created). Sometimes you want to make a class an abstract class, but there is just no pure virtual function. What to do? Because abstract classes are intended to be used as base classes, the base class must have a virtual destructor, and pure virtual functions produce abstract classes, so the method is simple: Declare a pure virtual destructor in the class that you want to be an abstract class.

Here is an example:

Class Awov {public   :       virtual ~awov () = 0;//Declare a pure virtual destructor};

This class has a pure virtual function, so it is abstract, and it has a virtual destructor, so there is no destructor problem. But here's one more thing: you must provide the definition of a pure virtual destructor:

Awov::~awov () {}//definition of pure virtual destructor

This definition is required because the virtual destructor works by first invoking the destructor of the underlying derived class, and then the destructor of each base class is called. This means that even if it is an abstract class, the compiler has to generate a call to ~awov, so be sure to provide it with a function body. If you do not do this, the linker will detect it, and finally have to go back and add it.

Article 9: Never call the virtual function during the tectonic destruction process

Virtual functions are not virtual functions in constructors, so there is a problem when using polymorphism, assuming first

Article 10 to implement a chain assignment, the assignment operator must return a reference to the argument to the left of the operator

widget& operator= (const widget& RHS) {... return *this;}

clause 12: Do not forget each part of the assignment when you write a copying function, you should do two points:

(1) Assigning all local variable members

(2) Call all copying functions of base class

Because you want to assign a value to an object, and also assign a value to the member objects in his parent class, these member objects are sometimes private and cannot be accessed directly, which is done through the copying function of the parent class, for example:

Base Classclass class1{public:class1 (const class1& C);class1& operator= (const class1&);p rivate:int p;}; CLASS1::CLASS1 (const class1& C1) {THIS->P=C1.P;} class1& class1::operator= (const class1& C1) {P=c1.p;return *this;} Derived Classclass class2:public class1{public:class2 (const class2&);class2& operator= (const class2&); Private:int pp;}; CLASS2::CLASS2 (const class2& C2): Class1 (C2), pp (c2.pp) {}class2& class2::operator= (const class2* c2) {class1: : operator= (C2);pp =c2.pp;return *this;}

Effective C + + essay (2)

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.