欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

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

发布时间:2025/3/21 c/c++ 57 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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-definedA(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; }

总结

以上是生活随笔为你收集整理的c++新特性11 (6) =default的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。