Effective C + + 2

Source: Internet
Author: User

Class A {public:a (); ~a (); a& operator= (const a& A); The copy is non-static member A (const A&A); };

When you declare a constructor, the default constructor is not generated automatically.
C + + does not allow reference to point to different objects.
If you want to avoid an object assignment, you can declare the assignment constructor in private.

The next thing to discuss is the virtual destructor problem.

Class timekeeper{
Public
Timekeeper ();
~timekeeper ();
...
};
Class Atomicclock:public timekeeper{};
Class Waterclock:public timekeeper{};
Class Wristwatch:public timekeeper{};

Timekeeper *gettimekeeper (); function, which returns a point to the Timekeepr derived class.

timekeeper* Ptk=gettimekeeper ();
Delete PTK; Call ~timekeeper (), which causes local deletion.
So the virtual destructor is introduced.

Virtual destructor Usage Location: When this class is a base class.
Conversely: A class that is used as a base class should have a virtual destructor to ensure that the destructor is correctly refactored.

The easiest way to make a class into an abstract class is to change the destructor to pure virtual and give a definition.

Class dbconnection{public:static dbconnection create (); void close (); Original Version class dbconn{public: ~dbconn () {db.close ();} private:dbconnection db; Force shutdown version Class dbconn{public: ~dbconn () {try{db.close ();} catch (...) {abort ();} } }; Ignore incorrect version class Dbconn{public: ~dbconn () {try{db.close ();} catch (...) {} } }; The final version class dbconn{public:void close ()//gives the customer a function that ends on its own. {db.close (); closed=true;} ~dbconn ()///If the customer does not call close, it ends itself (with the consequence) {if (!closed) {try{db.close ();} catch (...) {}}} Private:dbconnection db; BOOL closed; };

Virtual functions cannot be invoked in constructors and destructors, and are not as strong as above. At this point, virtual is a general function.

class, the base class constructor is called, the class constructor is derived, the derived class is refactored, and the base class is finally destructor.

To avoid code duplication, declare a private function, init (), and put the common code into the.

The assignment operator function (member function) is returned to Reference-to *this, and the assignment function of a non-member function cannot be returned reference

Class Widget {... private:bitmap*pb;}; widget& operator= (const widget& RHS) {if (rhs==&this) return *this; Delete pb;//delete First, if the following exception, it returns a problem. Pb=new Bitmap (RHS->PB); Still cannot resolve the exception caused by new. Return*this; } widget& operator= (const widget& RHS) {bitmap* ptmp=pb; pb=new Bitmap (RHS->PB);//New after Delete, if PB is not new successfully, Remain in the original state. If it succeeds, it's normal. Delete ptmp; return *this; The following is a grand introduction to copy and swap technology. void Widget::swap (widget& rhs); widget& operator= (const widget& ths) {Widget temp (RHS); swap (temp); return *this;}

Here's a joke, if you define the copying function yourself, if you make a mistake, the compiler will not remind you of the error in retaliation.

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.