Most of classes we write contain one or more constructors and destructors, which are responsible for controlling the fundamental operations of bringing new object into existence and making sure it’s initialized and getting rid of an object and making sure it’s cleaned up. Making mistakes in these functions will lead to unremarkable serious errors throughout the implemented classes, so in this post we are going to discuss some tips and tricks about how to deal with constructors and destructors that help in writing efficient C++ code.
Silent Calling and Writing of functions
It seems that you may implement an empty class in C++ like this
actually this is not an empty class as the compiler will generate its own version of copy constructor and copy assignment operator, moreover if you didn’t declare constructor the compiler will declare a default constructor and all of these functions will be public and inline ,so the previous line of code and the following code snippet are considered to be the same.
The following code will cause these functions to be generated
We can ask here, what should these functions do ?!
Regarding default Constructor and Destructor, they give the compiler a place to put behind the scenes code such as invocation of constructor and destructor of base classes and non-static data members, and this generated desturctor is non-virtual unless it’s for a class inheriting from base class that itself declares virtual destructor.
And regarding copy constructor and the copy assignment operator, the compiler-generated code simply copy each non-static data member of the source object to the target object.We can summarize all of this in one sentence
Compilers may implicitly generate a class’s default constructor, copy constructor, copy assignment operator and destructor.
From this sense we can ask how to disallow the use of compiler-generated functions that we don’t want ?!
The answer is very simple, we’ll declare the corresponding member functions private and won’t give them an implementation.
The user won’t be able to copy “Test” object , and if there’s a trial to copy an object the linker will complain and the error will be “can’t find appropiate default constructor”, but it’s possible to move the link-time error up to compile-time where the earlier error detection is always better than later by declaring the constructor and copy assignment operator private not in “Test” class but in a base class designed specifically to prevent copying
The compiler will try to generate a copy constructor or copy assignment operator if any body tries to copy a “Test” object, the compiler-generated versions of these function will try to call the base class functions, and this will be rejected as the copying operations are private.
To disallow functionality automatically provided by compilers, declare the corresponding member functions private and give no implementations.
That’s enough for today ! To Be Continued …
- Effective C++, Third Edition —Scott Meyers.
- Professional C++—