473,388 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Contructors and destructors problem

100 100+
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;
}



in
Sep 11 '07 #1
2 1353
gpraghuram
1,275 Expert 1GB
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";

}//raghu - destructor
};
int main(int argc, char* argv[])
{
{
e ee; //raghu - constructor
e ee1;//raghu - constructor
ee.f(ee1); //raghu - copy constructor
//printf("Hello World!\n");
//cout<<"Raghava"<<endl;
}
//raghu - destructor
//raghu - destructor
int t;
cin>>t;
return 0;
}



in
Hi,
See my comments with name raghu.
Raghuram
Sep 11 '07 #2
dmjpro
2,476 2GB
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???


Expand|Select|Wrap|Line Numbers
  1. class e
  2. {
  3.  
  4. public:
  5.     e()
  6.         {
  7.                 cout<<"Contructor\n";
  8.         }
  9.     ~e()
  10.         {
  11.                 cout<<"Destructor\n";
  12.         }
  13.     void f(e e1)
  14.         {
  15.                 cout<<"In e\n";
  16.         }
  17. };
  18. int main(int argc, char* argv[])
  19. {
  20.     {
  21.     e ee;
  22.     e ee1;
  23.     ee.f(ee1);
  24.     //printf("Hello World!\n");
  25.     //cout<<"Raghava"<<endl;
  26.     }
  27.     int t;
  28.     cin>>t;
  29.     return 0;
  30. }
  31.  

in
Use Code Tags while you do Post according to Posting Guidelines.
Look let me explain.
Constructors for object initialization, means the memory allocation for that Object. And destructor is for when Object is about to destroy.
And whether memory is more essential so Object destruction should be handled carefully.
Generally if your Object is not created using Pointer then your Run Time System will decided when your Object goes out of Life-Time Scope means then it is about to destroy.
Here your see two objects are created but look at this line carefully.
Expand|Select|Wrap|Line Numbers
  1. void f(e e1)//Here an Object is Copied using Copy Constructor.
  2. //If the line is ............. void f(e &e1), then no Object creation would be there.
  3.  
That's why three times the Object destruction happens.

Kind regards,
Dmjpro.
Sep 11 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
12
by: Ross Boylan | last post by:
I am trying to understand under what circumstances destructors get called with std::vector. I have an application in which I will put real objects, not just pointers, in the vector. 1. The...
3
by: rahul8143 | last post by:
hello, I write a following program and have problem in understanding constructors and destructors. #include <iostream.h> class vector { public: double x; double y;
26
by: Michi Henning | last post by:
I've been having problem with destructors in the context of having ported C# code developed under .NET to Mono. What happens is that, on a dual-CPU machine, various parts of the code crash randomly...
8
by: Edward Diener | last post by:
I have a __value class which uses some legacy C++ code. So I wrapped the legacy C++ code in another __nogc class and have a pointer to that class as a member of my __value class. When the __value...
17
by: Gabriel Zachmann | last post by:
Is it correct to say that the typical ownership problem, which frequently arises in C++, does not occur normally in Python? Best regards, Gabriel. --...
4
by: hyd | last post by:
With C++/CLI - VS2005, is it possible to have static constructors that are automatically called when the owner assembly is loading ? Otherwise, how it is possible to call it without creating an...
3
by: donna.lenharth | last post by:
Hello, I am debugging a C++ application that was ported from PostgreSQL to DB2 UDB (v8.1.0.112) on Linux and have run across a problem. We have a singleton object that handles connection...
6
by: Jeff Newman | last post by:
Hello, Could anyone explain to me why the following class's destructor shows up as having multiple branches? (At least as judged by gcov 4.1.2 when compiled with gcc 4.1.2 ): struct blah {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.