码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-04-06 09:52:27      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:基类   nbsp   col   line   div   拷贝   operator   出错   iostream   

#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没有默认构造)

 

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

标签:基类   nbsp   col   line   div   拷贝   operator   出错   iostream   

原文地址:https://www.cnblogs.com/douzujun/p/10660619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!