【面试】编译器为我们实现了几个类成员函数?(c++)

#include <cassert>
#include <complex>
#include <iostream>

class Empty{};

Empty e;
Empty b = e;
Empty d;
Empty b = d;
Empty f(b);

//c98--同上
class Empty2
{
    public:
        //默认构造
        Empty2() {}
        //拷贝构造
        Empty2(const Empty2&) {}
        //重载 = 
        Empty2& operator = (const Empty2&) {return *this;}
        //析构函数
        inline ~Empty2() {}
};

//编译器为我们实现了几个类成员函数
class AnotherEmpty : public Empty
{
    public:
        //同上, 只是在构造的时候,还会调用一下基类构造函数
        AnotherEmpty() : Empty() {}
};

class Void 
{
    public:
        //如果已经写过构造函数,编译器会把剩下的成员函数生成
        Void() {}
}


class NotEmpty
{
    public:
        //自己写了构造函数,编译器不会生成默认构造函数
        NotEmpty (int a) : m_value(a)  {}
    private:
        int m_value;
}

std::map<int, NotEmpty> m;
m[1] = NotEmpty(10);      //出错
//因为map会先查找key=1,有则返回其值的引用;没有,则默认插入一个NotEmpty,且用NotEmpty的默认构造函数(这里NotEmpty没有默认构造)

 

posted @ 2019-04-06 00:58  douzujun  阅读(266)  评论(0编辑  收藏  举报