Friday, 9 May 2014

Pointer To Member Operators

The pointer-to-member operators ->* and .* group left-to-right

Example of Pointer to Member Operators:



Example1: Pointer to member data
#include<cstdio>
class Test
{
public:
        int x;
        Test():x(5){}
        void display()
        {
                printf("x:%d\n",x);
        }
};
int main()
{
        Test obj;
        Test *objPtr=&obj;         //pointer to object "obj"
        int Test::*p = &Test::x;  //p is a pointer to member x of Test.
        obj.display();            // output:5
        obj.*p=7;                 //modify the content of x by using pointer p
        obj.display();            // output:7
        objPtr->*p=10;            //modify the content of x by using pointer p accessed by a pointer points to an object "obj".
        obj.display();            // output:10
}

Example2: Pointer to member function
#include<cstdio>
class Test
{
public:
        int x;
        Test():x(5){}
        void display()
        {
                printf("x:%d\n",x);
        }
};
int main()
{
        Test obj;
        Test *objPtr=&obj;                   //pointer to object "obj"
        void (Test::*ptr_display)();
        ptr_display = &Test::display;
        obj.display();                      //calling display function by object
        (obj.*ptr_display)();            //calling display function by pointer to member function
        (objPtr->*ptr_display)();           //calling display function by pointer to member function using object pointer
}

Friday, 2 May 2014

Implicit Definition in CPP

In some circumstances, C ++ implementations implicitly define the default constructor (12.1), copy constructor (12.8), move constructor (12.8), copy assignment operator (12.8), move assignment operator (12.8), or destructor (12.4) member functions.
--- N3690_CPP-11_Draft, Page-No: 33


class C
{

};
By default contains the implicit defined functions:
class C
{
public:
    C(){ }  //default constructor
    C(const C&)  //copy constructor
    {
    }
    C& operator=(const C& x)     //copy assignment operator
    {
        return *this;
    }
    ~C(){ } //destructor
};
In case if any constructor is explicitly defined inside the class, compiler will override the default constructor. But other implicit constructor will still be there until it is not explicitly overridden by the programmer. Example:
class C
{
public:
    C(C& obj){ }
};
contains the implicit defined functions:
class C
{
public:
    /*C(){ }*/  //no default constructor
    C(C& obj){ }   //explicitly defined by programmer
    C(const C&)  //copy constructor
    {
    }
    C& operator=(const C& x)     //copy assignment operator
    {
        return *this;
    }
    ~C(){ } //destructor
};

Saturday, 19 April 2014

Copy Constructor Dilemma: Is it possible to create a new object?

Consider the following lines of code
//ref1:
#ifndef __CAR_H__
#define __CAR_H__
#include <string.h>
class Car
{
private:
    Car& Object;
public:
    Car(Car& Obj):Object(Obj)
    {
    }
private:
    char color[20];
public:
    void setColor(char* color)
    {
        strcpy(this->color,color);
    }
    char* getColor()
    {
        return color;
    }
};
#endif


Since the class has defined a copy constructor only, it expects Car’s object to be passed to create any usable object. But we don’t have an object to create a new object. If this is the case then how do we create the first usable object of Car?
//ref2:

int main()
{
    Car object(object); // Is it ok? 
    object.setColor("blue");
    object.getColor();
}

How it works? When the statement on line number 5 is compiled, compiler will allocate memory of size Car. Which loader shall then map with actual memory of the system before executing program. This means memory segment has already been allocated before calling the constructor. Hence we have the address of uninitialized object Car.

Consequently we may pass the object to create a usable object of Car, as seen on line number 5 on ref2. And the compiler complies the program without complaining. As copy constructor requires an object reference of Car. This object being referred needn’t be initialized. Constructor doesn’t create the object, it only initializes the object calling the constructor.