C++ primer中的复制构造函数和运算符重载

#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 =
请按任意键继续. . .

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据