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.