内容目录
#include <stdio.h> #include <iostream> classCTest { public: CTest() : mInt(0) { printf("Default Constructor.\n"); } CTest(constCTest& copyCons) { printf("Copy Constructor.\n"); this->mInt = copyCons.getIntValue(); } ~CTest() {} voidprintIntValue() { printf("%d\n", mInt); } intgetIntValue() const { returnmInt; } voidoperator = (constCTest& copyCons) { printf("Operator =\n"); this->mInt = copyCons.getIntValue(); } private: intmInt; }; intmain() { CTest a; CTest b(a); CTest c = a; c = b; system("pause"); return0; }
输出:
Default Constructor.
Copy Constructor.
Copy Constructor.
Operator =
请按任意键继续. . .
Default Constructor.
Copy Constructor.
Copy Constructor.
Operator =
请按任意键继续. . .