0

Hi,
I the below code i could see 2 constructors and 3 destructors being called....i want to know why....can anyone help me in knowing this???

class e
{

public:
	e()
		{
				cout<<"Contructor\n";
		}
	~e()
		{
				cout<<"Destructor\n";
		}
	void f(e e1)
		{
				cout<<"In e\n";
		}
};
int main(int argc, char* argv[])
{
	{
	e ee;
	e ee1;
	ee.f(ee1);
	//printf("Hello World!\n");
	//cout<<"Raghava"<<endl;
	}
	int t;
	cin>>t;
	return 0;
}
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by WolfPack
0
ee.f(eel)

This line passes the object eel into function f by value. So f creates a copy of eel inside it. The constructor used here is a seperate constructor called the copy constructor. Because you have to defined it, C++ uses an implicitly defined default copy constructor and nothing is displayed. But underneath the scenes, this default copy constructor is called.

Define the class as below and see the output.

class e
{

public:
    e()
        {
                cout<<"Contructor\n";
        }
    e(e &e_instance)
        {
                cout<<"copy Contructor\n";
        }

    ~e()
        {
                cout<<"Destructor\n";
        }
    void f(e e1)
        {
                cout<<"In e\n";
        }
};
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.