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;
}
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";
        }
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.