compiler-generated default constructor
2014-08-05 10:51
281 查看
Concept:
Default constructor is the constructor that takes no argument, also called 0-argument constructor.
The compiler will generate the default constructor if the user does not implement ANY type of constructor. For example:
However,
To make Example 2 work, we need to provide the default constructor as well:
// Example 3
struct A
{
A() {}; // user defined default constructor
// with implementations in { }, even though it's void.
A( int i ) {};
int a;
}
A my_a; // Works! Default constructor is provided.
Or, to take advantage of C++11,
// Example 4
struct A
{
A() = default; // compiler-generated default constructor
// without the need of implementation ---> no { }
A( int i ) {};
int a;
}
A my_a; // Works! Default constructor is provided.
Default constructor is the constructor that takes no argument, also called 0-argument constructor.
The compiler will generate the default constructor if the user does not implement ANY type of constructor. For example:
// Example 1 struct A { int a; } A my_a; // Works! Compiler generates the default constructor.
However,
// Example 2 struct A { A( int i ) {}; int a; } A my_a; // Fails! Compiler doesn't generate the default constructor // because the user has specified one already.
To make Example 2 work, we need to provide the default constructor as well:
// Example 3
struct A
{
A() {}; // user defined default constructor
// with implementations in { }, even though it's void.
A( int i ) {};
int a;
}
A my_a; // Works! Default constructor is provided.
Or, to take advantage of C++11,
// Example 4
struct A
{
A() = default; // compiler-generated default constructor
// without the need of implementation ---> no { }
A( int i ) {};
int a;
}
A my_a; // Works! Default constructor is provided.
相关文章推荐
- Does compiler create default constructor when we write our own?
- C++对象模型(一):The Semantics of Constructors The Default Constructor (默认构造函数什么时候会被创建出来)
- No default constructor for entity解决
- Another form of default constructor
- (原創) 如何在constructor中使用default argument? (C/C++)
- CoreException: Could not get the value for parameter compilerId for plugin execution default-compile
- error C2600: 'Point::Point' : cannot define a compiler-generated special member function (must be de
- 继承Fragment后,重写构造函数就报:Avoid non-default constructors in fragments: use a default constructor plus Fra
- 面向对象程序设计-C++ Default constructor & Copy constructor& Destructor & Operator Overloading【第九次上课笔记】
- 构造函数的寓意:default constructor
- maven Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-com
- [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:31:compile (default-co
- fastjson解析json时报错 class not has default constructor
- Avoid non-default constructors in fragments: use a default constructor plus setArguments()错误提示解决方法
- Another form of default constructor
- 关于nontrivial default constructor(一)
- 可以使用可用的服务和参数调用在“eWorld.WCFImplement.ServiceImplement.ImageArchiveService”类型上使用“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”找到的构造函数: 无法解析参数'eWorld.WCFBLL.ImageArchive.IDocumentOperation
- 深入探索C++对象模型笔记之三 —— 构造函数语意学 (Default Constructor的建构操作)
- Android中Fragment的构造函数 his fragment should provide a default constructor
- hibernate生成数据表时报错:HHH000388: Unsuccessful: create table emp (empid integer generated by default as i