50 must-know things in C++ – part 5

It’s been a while since I haven’t wrote anything about C++…

23. Copy constructor – a special type of constructor

It is used to create a new object as a copy passed as parameter or used as a return value from a function. It is called also when an object is thrown or caught (if an exception occurs).

Another important thing to mention, which many beginner programmers usually miss it, is the difference with respect to assignment operator “=”.
Assignment operator is invoked in this case:

Object A;
Object B;
B = A;

Copy constructor is invoked in this case:

Object B = A;

Please note that in the first case a simple copy of A is simple copy of A is assigned to B. But what is a “simple copy” really meaning? For this please have a look on #24 >> Deep vs Shallow copy. If no copy constructor is explicitly declared (or if no assignment operator) this (also the assignment operator) will be generated by the compiler. This is not a reason to be happy, it should rather worry you. If you will have “complicated” objects, which will be passed as parameters or returned by value, you will have to carefully code the copy constructor.

Copy constructor and assignment operator generated by the compiler will just perform a shallow copy.

24. Deep copy vs shallow copy

A shallow copy is more or less the same thing with a bitwise copy. This discussion has to take place in cases where classes have pointer members, if this is not the case (corresponding class has only value data members) basically deep copy and shallow copy have the same meaning.

Basically in case of a shallow copy, pointers are copied into the newly created object, but not also the value that they point to. A deep copy manages to solve this.

To visually have a representation of what could happen if a shallow copy is performed over objects containing pointer members, I made the following drawing:

Suppose the two, Object1 and Object2, are instances of a class named Obj, which has a pointer member ptr.

Obj Object2 = Object1;  // this is the event which must produce either a deep or a shallow copy

As you can see in case of a shallow copy, ptr of Object2 is indicating the same data as ptr of Object1 does. This can have unwanted consequences. If ptr of Object1 is changing the data that is pointing to or if Object1 is going out of scope, ptr from Object2 may point to NOTHING, or to some garbage location

A deep copy manages to save the data that ptr of Object1 is pointing into another place and than makes the ptr of Object2 point to that place.

Let’s just have a brief on the example below:
class CX
{
public:

    int x;
    char *Sir;

CX (int x0, char *Sir0)
{

    x=x0;
    Sir = (char*)malloc(strlen (Sir0)+1);
    strcpy (Sir, Sir0);

}
~CX()
{

    free(Sir);

};
CX (CX&v)
{

    x=x.v;
    Sir=(char*)malloc(strlen (v.Sir)+1); //initial value pointed by Sir is copied and the new copied object will have its Sir pointing
    //to the new copied value
    strcpy (Sir, v.Sir);

};

};
int main()
{

    CX a(1,"string");
    CX b=a; //here the copy constructor is invoked
    return 0;

}

25. Private copy constructor and private assignment operator and … singletons – what are they?

As far as I read while “googling” on the Internet, private copy constructor is somehow a backdoor solution for programmers wishing to circumvent the implicit one provided by the C++ compilers.  There are cases when a class member refers to some data that needs not to be copied. How can you handle this?

If you want to keep a single copy of your class the solution is to declare the copy constructor as private.

The copy constructor as well as the implicit one generated by the compiler, are by default public, so relying on those is excluded right from the beginning.

What is a Singleton and how is this related to private constructors?  Wikipedia offers some explanations about it. Basically this is a class, or more generically, a design pattern, used to restrict the instantiation of a class to one object. In general Singletons have all the constructors and the assignment operator private and look like this.

Class Singleton{

public:

    Instance_method (); //method called to create an instance (the single one) of the class

    //since calling the constructor from main is not possible

private:

    Singleton() {};
    Singleton( Singleton const& ) {};
    Singleton& = operator (Singleton const&) {};

}

As I said previously there are cases where a single object is needed to act in a system, and you have to make sure that only one object exists, not many more.  Such situations may occur in a multi-thread environment where a singleton may act as a mutex.

An interesting explanation of what a singleton is and how can it be useful can be found here . While reading I thought of this phrase summarizing Singleton’s need:

The benefit of the Singleton pattern (which can be implemented in C++) is that it prevents you from filling up the global namespace. You can keep the variables and functions locked up inside a class and restrict access to it by implementing a single point of access, which prevents you from creating multiple instances of the class.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: