codescracker


c++

C++ Objects as Function Arguments



« Previous Tutorial Next Tutorial »


Objects are passed to functions in the same way as any other type of variable is passed. That is, object may be passed both ways : through call-by-value or through call-by-reference mechanism. Let's discuss these mechanisms.

C++ Passing Objects through Call-by-Value

When it is said that objects are passed through the call-by-value mechanism, it means that the called function creates a copy of the passed object. However, the fact that a copy is created means, in essence, that another object is created. This raises two related questions :

Let's take a look at the following C++ program to find out the answers of the above questions:

/* C++ Objects as Function Arguments */

#include<iostream.h>
#include<conio.h>
class Sample
{
	int x;
	public:
		Sample(int i)  // constructor
		{
			x = i;
			cout<<"Constructing Object with "<<i<<"\n";
		}
		~Sample()      // destructor
		{
			cout<<"Destroying Object having "<<x<<"\n";
		}
		void put_x(int i)
		{
			x = i;
		}
		int get_x(void)
		{
			return x;
		}
};
void afunc(Sample S1)
{
	S1.put_x(2) ;      // change value of x in the object
	cout<<"This is x local to afunc()\n";
	cout<<"x = "<<S1.get_x()<<"\n";
}
void main()
{
	clrscr();
	Sample S(1);      // create object with value 1
	cout<<"This is the x in main()\n";
	cout<<"x = "<<S.get_x()<<"\n";
	afunc(S);        // pass object S by value to afunc()
	cout<<"Back in main()\n";
	cout<<"x = "<<S.get_x()<<"\n";
	getch();
}

When the above C++ program is compile and executed, it will produce the following output:

c++ objects as function arguments

Notice that two calls to the destructor function are executed, but only one call is made to the constructor function. As it is clear from the output that the constructor function is not called when the copy of S is created by afunc(). The reason for this is when an object is passed to a function (through call-by-value mechanism), the current state of the object is passed. If the called function invokes the constructor, initialization (of the object) will occur, possibly changing the object. Thus, the constructor function is not involved when a function receives an object as its parameter rather it copies down the members of the passed object for the called function and that serves as a copy of the passed object.

Note - A called function receiving an object as a parameter creates the copy of the object without invoking the constructor. However, when the function terminates, it destroys this copy of the object by invoking its destructor function.

However, it is very much necessary to destroy the copy of the object that the called function was working with, before returning from the function. Thus the destructor function must be executed when this copy is destroyed.

C++ Passing Objects through References

As you know that when an object is passed by value to a function, a copy of that object is made without invoking its constructor. However, when the function terminates, the copy's destructor is called.

If you want the called function work with the original object so that there is no need to create and destroy the copy of it, you may pass the reference of the object. Then the called function refers to the original object using its reference or alias. The following program illustrates it :

/* C++ Objects as Function Arguments */

#include<iostream.h>
#include<conio.h>
class Sample
{
	int x;
	public:
		Sample(int i)
		{
			x = i;
			cout<<"Constructing Object with "<<i<<"\n";
		}
		~Sample()
		{
			cout<<"Destroying Object having "<<x<<"\n";
		}
		void put_x(int i)
		{
			x = i;
		}
		int get_x(void)
		{
			return x;
		}
};
void afunc(Sample &amp; S1)
{
	S1.put_x(2);
	cout<<"This is x local to afunc()\n";
	cout<<"x = "<<S1.get_x()<<"\n";
}
void main()
{
	clrscr();
	Sample S(1);
	cout<<"This is the x in main()\n";
	cout<<"x = "<<S.get_x()<<"\n";
	afunc(S);
	cout<<"Back in main()\n";
	cout<<"x = "<<S.get_x()<<"\n";
	getch();
}

When the above C++ program is compile and executed, it will produce the following output:

passing objects through call by value reference c++

Now compare this output with the output of the previous program. Contrary to the previous output, here only one call has been made to constructor function and one call to destructor function. That is because when the object is passed to the function by reference, the formal parameter (S1 here) of afunc() becomes the reference (alias) of actual parameter (S here). That means the called function afunc() refers to the original object with an alias name; it does not make a copy of the passed object. Thus, no constructor function is invoked since no new object has been created.

Also, when function afunc() is terminated, no destructor is invoked since there does not exist any copy of the object exclusively for afunc() which is to be destroyed.

However, the constructor is invoked when the object S is declared by the statement Sample S(1); and destructor is invoked when this object S is destroyed, that is, when main() terminates.

When passing parameters by reference, remember that changes to the object inside the function affect the calling object.


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test