Normally, ctor , destructor and assignment should be public.In inheritance context, all the base class desctructor shouldbe virtual.
The Default ctor, Copy ctor and Assignment operator will not be implicitly
generated if
you have explicitly declared any constructor. Or copy ctor and
Assigmnet operaor(for class X a constructor taking X, X& or const
X&).
There is a member in your class that is not default-constructible
(such as a reference, a const object, or a class with no or
inaccessible default constructor)
(C++11) you have explicitly told the compiler to not generate one
using A() = delete;
The Destructor will not be implicitly generated if
you have explicitly declared a destructor
(C++11) you have explicitly told the compiler to not generate one
using A() = delete;
The Move Constructor or Move Operator(C++11) will not be implicitly
generated if
you have explicitly declared a move constructor or move
assignment(for class X, a constructor taking X&&)
there is a member in your class that cannot be moved (have
deleted, inaccessible, or ambiguous)
you have defined a copy assignment operator, copy constructor,
destructor, or move assignment operator
you have explicitly told the compiler to not generate one using
A(A&&) = delete;
Even you write Empty class, compiler will produce at least five member
functions.
Sometimes you dont’ want compiler to produce these member function
automatically, such as you define a array class. you don’t want client to use
array1=array2(It’s not allow in common c/c++ language). You can just
declare you own assignment operator as private member function,
and you don’t need to implement it. (If you use it, linker will yelp
at you. ) A example can be seen effective c++ item "Explicitly
disallow use of implicityly generated member functions you don’t
want". Another way is to use =delete, see last chapter " C++ new
features"
Make Constructors Protected to prohibit direct Instantiation. Make
constructors Private to prohibit Derivation.
Use default arguments to reduce the number of ctor.
If you define a specify ctor, you also need to define default ctor.
Because system will not produce any default ctor for you. So below
statement will produce error when compiling.
From previous example, you can see that default ctor is very important. but
when class MUST need another information when create, such as
worker class, You must provide SSN when you create a worker. At
this time, if you create default ctor, It’s not good idea. A NULL
SSN will cause a lot of trouble in the future. That is C++ spirit,
You never have the best answer, only have context answer.
copy ctor, assignment operator, and destructor three function should be exit
at the same time. The perform deep copy if you have new action
inside of class. or you will perform shallow copy, which sometimes is
dangerous.
When to use member initalize list, you can see in this link:
to non-static const data members.
reference member
member objects which do not have default constructor: (why I
need default ctor can be explained here too)
need pass argument to base class ctor
need to by copy between obj to member obj. (only one copy ctor,
more efficient! see below source code.)
In previous example, you can see when you pass and return value of obj, It
will call copy ctor, It’s not very efficient. So you should use reference or
pointer if it’s possible, don’t pass object directly.
An Assignment operator examples: 1) avoid assignment self 2) return*this reference.