zoukankan      html  css  js  c++  java
  • 【面试】编译器为我们实现了几个类成员函数?(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没有默认构造)

  • 相关阅读:
    JAVA窗口程序实例一
    内存容量出现异常的解决办法
    Android Studio 2.0使用指南
    Android 开机自启服务
    Android 图形总结
    今日课堂总结
    JSON
    GET方法和POST方法
    ajax的简单介绍
    网页-豌豆上的公主
  • 原文地址:https://www.cnblogs.com/douzujun/p/10660619.html
Copyright © 2011-2022 走看看