Friday, November 13, 2009

Accessing the Private Member outside the Class (without Friend)

The following article discusses the dangers of returning a reference to private data of a class.

First, it is shown how a reference to private data member is returned from a member function and then the consequences of doing so are discussed.

Returning a Reference to Private Data in a Class :

The example below shows how a reference is returned to a private data member in a class :

class ATestClass
{
private:
int x ; // these are private
char y; // data members of the class

public:
AtestClass () // default constructor of class
{
--------------------------------
---body of constructor----
--------------------------------
}

int &retReference() // this is the bad function that returns reference to private member x
{
x = 10;
return x ; // dangerous !! (reference to private data x is returned)
}
};

The catch in the above code snippet is that the function retReference() can be used as an lvalue in any assignment statement. The code snippet below shows how :

int main()
{
ATestClass testObject; // creates an object of ATestClass
testObject.retReference() ; // sets the value of x in testObject as 10
int z = testObject.retReference() ; // z becomes 10
testObject.retReference() = 20 ; // dangerous !! the private data member of ATestClass is directly accessed and modified !
int &a = testObject.retReference() ; //again dangerous !! a becomes a reference to private data x in object testObject
a = 100 ; // dangerous !! private data member x in object testObject

return 0 ;

}

In the above code snippet, the statement

testObject.retReference() = 20 ;
is a perfectly valid C++ statement because the function retReference() returns a reference to int x, and hence it can be used as an lvalue in any assignment statement. Thus, even if data member x is private in class ATestClass, it is directly accessed through object testObject and is modified.

Similarly, the statement

int &a = testObject.retReference();
declares a as a reference to private data member x in testObject. Then, the following statement
a = 100 ;
uses a as an alias (reference) to private data x and modifies its value.

This breaks-down the data hiding and encapsulation features of a class and defeats the purpose of Object OrientedProgramming in the following ways :

  • The private data in the class no longer remain “private” as they can be directly accessed through any variable that is a reference to the private data.
  • The encapsulation feature is also destroyed as data and its functions are no longer bound together. Data can be accessed without the help of the member functions of the class.

Thus, we have seen how a simple thing such as returning references to private data members of a class destroy Object Oriented Programming concepts. This is an example of a poor programming practice, and should be avoided.

Friend Function & Class

Any data which is declared private inside a class is not accessible from outside the class. A function which is not a member or an external class can never access such private data. But there may be some cases, where a programmer will need need access to the private data from non-member functions and external classes. C++ offers some exceptions in such cases.

A class can allow non-member functions and other classes to access its own private data, by making them as friends. This part of C++ tutorial essentially gives two important points.

  • Once a non-member function is declared as a friend, it can access the private data of the class
  • similarly when a class is declared as a friend, the friend class can have access to the private data of the class which made this a friend
Let's see a sample in this C++ tutorial for each of the above cases.

C++ Tutorial - Friend function sample:

#include
//Declaration of the function to be made as friend for the C++ Tutorial sample
int AddToFriend(int x);
class CPP_Tutorial
{
int private_data;
friend int AddToFriend(int x);
public:
CPP_Tutorial()
{
private_data = 5;
}
};
int AddToFriend(int x)
{
CPP_Tutorial var1;
return var1.private_data + x;
}
int main()
{
cout << "Added Result for this C++ tutorial: "<< AddToFriend(4)< }

The output of the above C++ Tutorial sample will be
Added Result for this C++ tutorial: 9

C++ tutorial - friend class:

Declaration of a friend class is also similar. Only thing is a class definition is slightly different.

C++ Tutorial - Friend function:

#include <>
class CPP_Tutorial
{
int private_data;
friend class friendclass;
public:
CPP_Tutorial()
{
private_data = 5;
}
};
class friendclass
{
public:
int subtractfrom(int x)
{
CPP_Tutorial var2;
return var2.private_data - x;
}
};
int main()
{
friendclass var3;
cout << "Added Result for this C++ tutorial: "<<>
}

The output of the above C++ Tutorial sample will be
Subtracted Result for this C++ tutorial: 3

This is a good way out given by C++ to avoid restrictions on private variables. But this should be used with caution though. If all the functions and classes are declared as friends, then the concept of encapsulation and data security will go for a toss.

That is why the concept of friend functions and classes should be used with proper judgment.

Static Functions

Static data types can be accessed without instantiation of the class in C++. This is applicable for static functions also.

The differences between a static member function and non-static member functions are as follows.
  • A static member function can access only static member data, static member functions and data and functions outside the class. A non-static member function can access all of the above including the static data member.
  • A static member function can be called, even when a class is not instantiated, a non-static member function can be called only after instantiating the class as an object.
  • A static member function cannot be declared virtual, whereas a non-static member functions can be declared as virtual
  • A static member function cannot have access to the 'this' pointer of the class.
The static member functions are not used very frequently in programs. But nevertheless, they become useful whenever we need to have functions which are accessible even when the class is not instantiated.

Copy constructor

Copy constructor is
  • a constructor function with the same name as the class
  • used to make deep copy of objects.

There are 3 important places where a copy constructor is called.

  1. When an object is created from another object of the same type
  2. When an object is passed by value as a parameter to a function
  3. When an object is returned from a function

If a copy constructor is not defined in a class, the compiler itself defines one. This will ensure a shallow copy. If the class does not have pointer variables with dynamically allocated memory, then one need not worry about defining a copy constructor. It can be left to the compiler's discretion.

But if the class has pointer variables and has some dynamic memory allocations, then it is a must to have a copy constructor.


For ex:
class A //Without copy constructor
{
private:
int x;
public:
A() {A = 10;}
~A() {}
}

class B //With copy constructor
{
private:
char *name;
public:
B()
{
name = new char[20];
}
~B()
{
delete name[];
}
//Copy constructor
B(const B &b)
{
name = new char[20];
strcpy(name, b.name);
}
};

Let us Imagine if you don't have a copy constructor for the class B. At the first place, if an object is created from some existing object, we cannot be sure that the memory is allocated. Also, if the memory is deleted in destructor, the delete operator might be called twice for the same memory location.

This is a major risk. One happy thing is, if the class is not so complex this will come to the fore during development itself. But if the class is very complicated, then these kind of errors will be difficult to track.

In Windows this will lead to an application popup and unix will issue a core dump. A careful handling of this will avoid a lot of nuisance

Virtual Functions

What is a Virtual Function?

A virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. It is one that is declared as virtual in the base class using the virtual keyword. The virtual nature is inherited in the subsequent derived classes and the virtual keyword need not be re-stated there. The whole function body can be replaced with a new set of implementation in the derived class.

What is Binding?

Binding refers to the act of associating an object or a class with its member. If we can call a method fn() on an object o of a class c, we say that the object o is binded with the method fn(). This happens at compile time and is known as static or compile - time binding. The calls to the virtual member functions are resolved during run-time. This mechanism is known as dynamic binding. The most prominent reason why a virtual function will be used is to have a different functionality in the derived class. The difference between a non-virtual member function and a virtual member function is, the non-virtual member functions are resolved at compile time.

How does a Virtual Function work?

Whenever a program has a virtual function declared, a v - table is constructed for the class. The v-table consists of addresses to the virtual functions for classes that contain one or more virtual functions. The object of the class containing the virtual function contains a virtual pointer that points to the base address of the virtual table in memory. Whenever there is a virtual function call, the v-table is used to resolve to the function address. An object of the class that contains one or more virtual functions contains a virtual pointer called the vptr at the very beginning of the object in the memory. Hence the size of the object in this case increases by the size of the pointer. This vptr contains the base address of the virtual table in memory. Note that virtual tables are class specific, i.e., there is only one virtual table for a class irrespective of the number of virtual functions it contains. This virtual table in turn contains the base addresses of one or more virtual functions of the class. At the time when a virtual function is called on an object, the vptr of that object provides the base address of the virtual table for that class in memory. This table is used to resolve the function call as it contains the addresses of all the virtual functions of that class. This is how dynamic binding is resolved during a virtual function call.

The following code shows how we can write a virtual function in C++ and then use the same to achieve dynamic or runtime polymorphism.

#include
class base
{
public:
virtual void display()
{
cout<<”\nBase”;
}
};
class derived : public base
{
public:
void display()
{
cout<<”\nDerived”;
}
};

void main()
{

base *ptr = new derived();
ptr->display();
}

In the above example, the pointer is of type base but it points to the derived class object. The method display() is virtual in nature. Hence in order to resolve the virtual method call, the context of the pointer is considered, i.e., the display method of the derived class is called and not that of the base. If the method was non virtual in nature, the display() method of the base class would have been called.

Virtual Constructors and Destructors

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor.

A virtual destructor is one that is declared as virtual in the base class and is used to ensure that destructors are called in the proper order. It is to be remembered that destructors are called in the reverse order of inheritance. If a base class pointer points to a derived class object and we some time later use the delete operator to delete the object, then the derived class destructor is not called. Refer to the code that follows:

#include
class base
{
public:
~base()
{

}
};

class derived : public base
{
public:
~derived()
{

}
};

void main()
{

base *ptr = new derived();
// some code
delete ptr;
}

In this case the type of the pointer would be considered. Hence as the pointer is of type base, the base class destructor would be called but the derived class destructor would not be called at all. The result is memory leak. In order to avoid this, we have to make the destructor virtual in the base class. This is shown in the example below:

#include
class base
{
public:
virtual ~base()
{

}
};

class derived : public base
{
public:
~derived()
{

}

};

void main()
{

base *ptr = new derived();
// some code
delete ptr;
}

Conclusion:

Virtual methods should be used judiciously as they are slow due to the overhead involved in searching the virtual table. They also increase the size of an object of a class by the size of a pointer. The size of a pointer depends on the size of an integer. Note that in DOS based systems the size of a pointer is 2 bytes whereas in UNIX based systems it is 4 bytes.