Effective C + + notes (4)

Source: Internet
Author: User

---------------------------15/03/29----------------------------


#9 never turn the virtual function in the process of construction and destruction

{

/*

1: The virtual function is called at construction time with two results

1> If the base class implements this function, it calls the function of the base class.

2> base class does not implement this function, link times wrong.

reason :

The constructor for the 1> base class is called earlier than the constructor of the derived class, so if the virtual function uses the derived class version of the

, it is possible to use a member variable of a derived class that is not initialized. C + + in order to not let you go this dangerous road, will only call the base class version

2> root cause : When the base class constructor is called, the object type is baseclass , so the base version is called.

so do not call any virtual functions in the constructor .

2: Call the Vitual function in the destructor :

because the destructor for a derived class is called before the base class, the member variables of the derived class are still present when the constructor for the base class is round.

no state is defined. So C + + sees that they do not exist, and when the base class destructor is called, the object is called a base class object.

3: in order for a derived class object to be created, there is an appropriate version of the display information call in the base class, and a workaround is to pass in a message by the derived class.

then call the Non_virtual version to display.

*/

}


#10 make operator= return a reference to *this

{

/*

With regard to assignment,C + + can be written in a chain form:

int x, y, Z;

x = y = z = 15;

in order to implement this chain assignment, the assignment operator must return a reference to the left argument of the operator.

*/

class Widgets

{

public:

widget&operator= (const widget& RHS)

{

...

return *this;

}

};

}


#11 Handling Self-assignment in operator=

{

// See the following function

widget&

Widget::operator= (const widget& RHS)

{

Delete PB;

PB =new Bitmap (*RHS.PB);

return *this;

}

// So if the self-assignment, then the first delete pb, the last PB point to a deleted object

// if the operator= has exceptional security , it is often automatically self-assigning security.

widget& Widget::operator= (const widget& RHS)

{

bitmap* Porig = PB;

PB =new Bitmap (*RHS.PB);

Delete Porig;

return *this;

}

// This will have an efficiency problem: if it is self-assignment, then do a new and delete, but consider the probability of self-assignment

// It is not recommended to add if judgment at the beginning to determine whether you are yourself. And with an if statement, the efficiency will drop significantly.

// There is another way to ensure exceptional security with swap

widget& Widget::operator= (const widget& RHS)

{

Widget temp (RHS);

Swap (temp);

return *this;

}

}


//#12 do not forget every ingredient when copying an object

{

/*

1: When you implement the operator= operation yourself, if you forget to copy a member variable, the compiler will not warn you

2: When you write your own operator= or copy constructor, you should remember:

1> Copy all of its own member variables

2> calls all the appropriate copying functions within the base classes .

3: If the copy constructor and the copy assignment operator are found to have similar code, the practice of eliminating duplicate code is

Create an init member function for both calls. ( But considering the previous entry, the constructor's direct initialization is highly efficient, so

If the similar code is only assigned, it is more hands-on good )

*/

Prioritycustomer::P rioritycustomer (const prioritycustomer& RHS)

: Customer (RHS), priority (Rhs.priority)

{

...

}

prioritycustomer&

Prioritycustomer::operator= (const prioritycustomer& RHS)

{

...;

Customer::operator= (RHS);

Prioriy = rhs.priority;

return *this;

}

}












Effective C + + notes (4)

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.