GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Sep-2007, 21:34
rag84dec rag84dec is offline
New Member
 
Join Date: Jun 2007
Posts: 11
rag84dec is an unknown quantity at this point

Contructors and destructors problem


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???


CPP / C++ / C Code:
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;
}
Last edited by LuciWiz : 17-Sep-2007 at 13:50. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 10-Sep-2007, 23:45
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: Contructors and destructors problem


In C++, unlike in some languages, objects are not automatically passed by reference. So the function
CPP / C++ / C Code:
void f(e e1)
is passed a copy of the object, which means the copy constructor is invoked to create the copy.

Since your class doesn't define a copy constructor, you don't get notified of the object's construction. Try adding this to your class:

CPP / C++ / C Code:
e(const e& eCopy )
{
    cout << "Copy constructor\n";
}

Find a book or some online reference about classes, they should tell you about why and when you need a copy a constructor and an assignment operator.
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:07.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.