C++默认函数罗列

C++类默认生成的函数有哪些,能一口气写全写正确并不是那么简单的事,好记性不如烂笔头,罗列C++的默认函数方便速查。

class Test
{
public:
    //无参数默认构造函数
    Test()
    {
        cout << "test1" << endl;
    }
    //拷贝构造函数
    Test(const Test &t)
    {
        cout << "test2" << endl;
    }
    //赋值构造函数
    Test &operator=(const Test &t)
    {
        cout << "test3" << endl;
    }
    //取址
    Test *operator&()
    {
        cout << "operator1" << endl;
        return this;
    }
    const Test *operator&() const
    {
        cout << "operator2" << endl;
        return this;
    }
    //析构
    ~Test()
    {
        cout << "destructor" << endl;
    }
};

实验代码:

int main()
{
    Test t1;
    Test t2(t1);
    Test t3 = t1;
    Test t4;
    t4 = t1;

    Test *p1 = &t1;
    const Test *p2 = &t2;

    const Test &tt = t1;
    const Test *p3 = &tt;

    return 0;
}

输出结果:

test1
test2
test2
test1
test3
operator1
operator1
operator2
destructor
destructor
destructor
destructor
发表于 2014年05月05日 23:40   评论:0   阅读:2575  



回到顶部

首页 | 关于我 | 关于本站 | 站内留言 | rss
python logo   django logo   tornado logo