compiler-generated default constructor

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:

// 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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值