【面试】编译器为我们实现了几个类成员函数?(c++)
生活随笔
收集整理的這篇文章主要介紹了
【面试】编译器为我们实现了几个类成员函数?(c++)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#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://默認(rèn)構(gòu)造
Empty2() {}//拷貝構(gòu)造Empty2(const Empty2&) {}//重載 = Empty2& operator = (const Empty2&) {return *this;}//析構(gòu)函數(shù)inline ~Empty2() {}
};//編譯器為我們實現(xiàn)了幾個類成員函數(shù)
class AnotherEmpty : public Empty
{public://同上, 只是在構(gòu)造的時候,還會調(diào)用一下基類構(gòu)造函數(shù)
AnotherEmpty() : Empty() {}
};class Void
{public://如果已經(jīng)寫過構(gòu)造函數(shù),編譯器會把剩下的成員函數(shù)生成
Void() {}
}class NotEmpty
{public://自己寫了構(gòu)造函數(shù),編譯器不會生成默認(rèn)構(gòu)造函數(shù)NotEmpty (int a) : m_value(a) {}private:int m_value;
}std::map<int, NotEmpty> m;
m[1] = NotEmpty(10); //出錯
//因為map會先查找key=1,有則返回其值的引用;沒有,則默認(rèn)插入一個NotEmpty,且用NotEmpty的默認(rèn)構(gòu)造函數(shù)(這里NotEmpty沒有默認(rèn)構(gòu)造)
?
轉(zhuǎn)載于:https://www.cnblogs.com/douzujun/p/10660619.html
總結(jié)
以上是生活随笔為你收集整理的【面试】编译器为我们实现了几个类成员函数?(c++)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NDK学习笔记-多线程与生产消费模式
- 下一篇: Petrozavodsk Winter