The compiler default class member function to achieve

The following situations compiler will generate a class member function as follows:

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

// 编译器为我们实现了几个类成员函数?
class Empty {};
//Empty e;
//Empty b=e;
//Empty d;
//Empty b=d;
//Empty F(b);
//Empty* operator&();                 //取址运算符
//const Empty* operator&() const;     //取址运算符(const版本)
//有默认构造函数、拷贝构造函数、析构函数、赋值语句、取址运算符、取址运算符(const版本)

// 编译器为我们实现了几个类成员函数?
class AnotherEmpty : public Empty {};
//有默认构造函数、拷贝构造函数、析构函数、赋值语句、取址运算符、取址运算符(const版本)
//只是构造的时候会调用基类的构造函数

// 编译器为我们实现了几个类成员函数?
class Void {
	public:
		Void() {}
};
//拷贝构造函数、析构函数、赋值语句、取址运算符、取址运算符(const版本),除了构造函数

// 编译器为我们实现了几个类成员函数?
class NotEmpty {
	public:
		NotEmpty(int a) : m_value(a) {}
	private:
		int m_value;
};
//拷贝构造函数、析构函数、赋值语句、取址运算符、取址运算符(const版本),除了构造函数
//如果提供了自己的构造函数C++就不会实现默认构造函数

std::map<int, NotEmpty> m;
m[1] = NotEmpty(10);      //出错
//[]要求类必须有自身的默认构造函数,否则编译报错
//因为map会先查找key=1,有则返回其值的引用;没有,则默认插入一个NotEmpty,且用NotEmpty的默认构造函数(这里NotEmpty没有默认构造)
Published 161 original articles · won praise 44 · views 7964

Guess you like

Origin blog.csdn.net/qq_39885372/article/details/104121066