c++新特性11 (6) =default

1. 使用=default来要求编译器生成一个默认构造函数

struct Point{
    
     
    Point() = default;//不用像下面的构造函数一样要一一对成员变量赋值
    Point(int _x, int _y):x(_x),y(_y){
    
    }
    int x=0;
    int y=0;
};

eg.

// use of defaulted functions
#include <iostream>
using namespace std;

class A {
    
    
public:
    // A user-defined
    A(int x){
    
    
        cout << "This is a parameterized constructor";
    }

    // Using the default specifier to instruct
    // the compiler to create the default implementation of the constructor.
    A() = default;
};

int main(){
    
    
    A a;          //call A()
    A x(1);       //call A(int x)
    cout<<endl;
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/thefist11cc/article/details/123886968
今日推荐