Copy Constructor and ASSIGN Construction Function

xiaoxiao2021-03-06  106

Copy constructor and ASSIGN constructor are called very interesting, there is a fun example below

#include #include using namespace std; class cat {public: cat (int A): Num (a) {cout << "constructor ...." << Endl;} cat (const) CAT & RSH) {Num = RSH.NUM; COUT << "Copy Construtor Called / N";} cat & operator = (const cat & rsh) {cout << "assigning constructor caled << endl; Num = RSH.NUM; RETURN * this;} ~ cat () {} private: int Num;}; int main (int Argc, char * argv [}) {cat A (10); Cat B (20); cout << "CAT D = A / N "; CAT D = A; // <----- Here, it is called, regarded as Cat D (a) d = a; system (" pause "); return 0;}

Actual operation

CAT D = a; When the compiler is processed, it does not turn to CAT D; D = A; but directly as Cat D (a) is processed. Only the ASSIGN constructor is implemented only if it is true, I think this should be the result of the compiler for efficiency, or the C grammar itself, this is not known.

转载请注明原文地址:https://www.9cbs.com/read-97790.html

New Post(0)