类的一些默认成员函数

 

//1.编译器为我们实现了哪些类成员函数

class Empty {};

//C++ 98 会有如下函数
    public:
        Empty2() {} //默认构造函数

        Empty2(const Empty2&) {}//默认复制构造函数

        Empty2& operator = (const Empty2&) { return *this; }//默认=重载

        inline ~Empty2() {}//析构




//2.编译器为我们实现了哪些类成员函数  

class AnotherEmpty : public Empty {};
//C++ 98 会有如下函数

    public:
        AnotherEmpty() : Empty() {} //默认构造函数,会调用基类的构造函数

        AnotherEmpty(const AnotherEmpty&) {}//默认复制构造函数

        AnotherEmpty& operator = (const AnotherEmpty&) { return *this; }//默认=重载

        inline ~AnotherEmpty() {}//析构




//3.编译器为我们实现了哪些类成员函数  

class NotEmpty {
    public:
        NotEmpty(int a) : m_value(a) {}
    private:
        int m_value;
};

//C++ 98 会有如下函数

    public:
        NotEmpty(const NotEmpty&) {}//默认复制构造函数

        NotEmpty& operator = (const NotEmpty&) { return *this; }//默认=重载

        inline ~NotEmpty() {}//析构

//当存在构造函数时,就不会生成默认构造函数


//引申:
std::map<int, NotEmpty> m;
m[1] = NotEmpty(10);
//此处会编译错误,对于map中括号的操作:
//首先会在1处看是否存在这个值,如果有返回其引用
//如果没有,则会调用默认构造函数即NotEmpty()然后插入,
//但是此时的NotEmpty没有NotEmpty()这个构造函数

猜你喜欢

转载自blog.csdn.net/whiskey_wei/article/details/81143977