Special member functions (constructor) - C++11, 16 of n

Special member functions:

  1. default constructor
  2. copy constructor
  3. copy assignment operator
  4. Move constructor
  5. Move assignment operator
Constructors
  • A constructor can be invoked for a const, volatile or const volatile object.const and volatile semantics are not applied on an object under construction. They come into effect when the constructor for the most derived object ends.
  • A return statement in the body of a constructor shall not specify a return value.The address of a constructor shall not be taken.
  • A defaulted default constructor for class X is defined as deleted if
    1. X is a union-like class that has a variant member with a non-trivial default constructor,
    2. any non-static data member with no brace-or-equal-initializer ({} OR =) is of reference type,
    3. any non-variant non-static data member of const-qualified type (or array thereof) with no brace-or-equal-initializer does not have a user-provided default constructor,
    4. X is a union and all of its variant members are of const-qualified type (or array thereof),
    5. X is a non-union class and all members of any anonymous union member are of const-qualified type (or array thereof),
    6. any direct or virtual base class, or non-static data member with no brace-or-equal-initializer, has class type M (or array thereof) and either M has no default constructor or overload resolution as applied to M’s default constructor results in an ambiguity or in a function that is deleted or inaccessible from the defaulted default constructor, or
    7. any direct or virtual base class or non-static data member has a type with a destructor that is deleted or inaccessible from the defaulted default constructor.
A default constructor is trivial if it is not user-provided and if:
  • its class has no virtual functions and no virtual base classes, and
  • no non-static data member of its class has a brace-or-equal-initializer, and
  • all the direct base classes of its class have trivial default constructors, and
  • for all the non-static data members of its class that are of class type (or array thereof), each such class has a trivial default constructor.
Otherwise, the default constructor is non-trivial.
____________________________________________

#include <memory>
#include <vector>
#include <string>

using namespace std;

union U {
public:
    U() = default;

    unique_ptr<vector<string>> pv_;
    shared_ptr<vector<string>> pv2_;
};

class A {
private:
    ~A() = default;
};

class B {
public:
    //B() = default;
};

class C {
public:
    C() = default;

private:
    //A a_;    // case 7
    //B& b_;   // case 2
    //const B cb_; // case 3
};

union CONST_U {
public:
    CONST_U() = default;
    const int i_;
    const long l_;
};

class D {
public:
    D() = default;

private:
    union {
        const int i_;
        const long l_;
    } u_;
};

class M {
public:
    M() = delete;
};

class E : public M {
public:
    E() = default;
};

int main() {
    //U u;  // case 1
    //C c;  // case 2, 3, 7
    // CONST_U cu; // case 4
    //D d; // case 5
    //E e;  // case 6, 7

    return 0;
}

About  brace-or-equal-initializerin a class definition:
In C++11, we can initialize the data member variables as below:

class F {
public:
    void print() {
        cout << "i = " << i << ", j = " << j << "\n";
    }

private:
    int i = 10;  // equal initializer
    int j {11};  // brace initializer
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值