Special member functions:
- default constructor
- copy constructor
- copy assignment operator
- Move constructor
- 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
- X is a union-like class that has a variant member with a non-trivial default constructor,
- any non-static data member with no brace-or-equal-initializer ({} OR =) is of reference type,
- 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,
- X is a union and all of its variant members are of const-qualified type (or array thereof),
- X is a non-union class and all members of any anonymous union member are of const-qualified type (or array thereof),
- 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
- 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.
____________________________________________
#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;
};
#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
//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
};
class F {
public:
void print() {
cout << "i = " << i << ", j = " << j << "\n";
}
private:
int i = 10; // equal initializer
int j {11}; // brace initializer
};