473,396 Members | 1,918 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,396 software developers and data experts.

problem in calling constructors and destructors

hello,
I write a following program and have problem in understanding
constructors and destructors.
#include <iostream.h>
class vector
{
public:

double x;
double y;
static int cnt,dst;
vector (double = 0, double = 0);
~vector ();
vector operator + (vector);
};

int vector::cnt=1;
int vector::dst=1;
vector::~vector ()
{
cout<<"called destructor "<<dst++<<"times\n";
}

vector::vector (double a, double b)
{
cout<<"called constructor "<<cnt++<<"times\n";
x = a;
y = b;
}

vector vector::operator + (vector a)
{
return vector (x + a.x, y + a.y);
}

ostream& operator << (ostream& o, vector a)
{
o << "(" << a.x << ", " << a.y << ")\n";
return o;
}

int main ()
{
vector a;
vector b;
vector c (3, 5);
a = b + c;
cout << "The content of vector a: " << a;
return 0;
}

/*
called constructor 1times
called constructor 2times
called constructor 3times
called constructor 4times
called destructor 1times
called destructor 2times
The content of vector a: (3, 5)
called destructor 3times
called destructor 4times
called destructor 5times
called destructor 6times
*/
From above program what i get is that class objects a,b,c calls

constructors 1,2,3 then function operator + (vector a) calls
constructor 4 but why this function corresponds to 2 destructor
functions?
Again it can be easily understood that constructors 1,2,3 has
corresponding destructors 4,5,6 then for what purpose destructor 3
called.
I am referring my output for numbering constructors and destructors.

Aug 5 '05 #1
3 1717

ra*******@gmail.com wrote:

[]
From above program what i get is that class objects a,b,c calls

constructors 1,2,3 then function operator + (vector a) calls
constructor 4 but why this function corresponds to 2 destructor
functions?
Again it can be easily understood that constructors 1,2,3 has
corresponding destructors 4,5,6 then for what purpose destructor 3
called.


To see the whole picture implement and add logging in
vector::vector(vector const&) and vector& operator=(vector const&);

Aug 5 '05 #2
ra*******@gmail.com wrote:
[]
From above program what i get is that class objects a,b,c calls

constructors 1,2,3 then function operator + (vector a) calls
constructor 4 but why this function corresponds to 2 destructor
functions?
Again it can be easily understood that constructors 1,2,3 has
corresponding destructors 4,5,6 then for what purpose destructor 3
called.
I am referring my output for numbering constructors and destructors.


Do You have a default constructor with 'cout<<"called constructor
"<<cnt++<<"times\n";' instruction? I don't see it in your code, but you
assume that it should print text due to construct objects a and b. I'm
sure you have not copy constructor with print instruction.
First of all your operator+ and operator<< work with *values* and not
*references*. It should looks like this:

vector& vector::operator + (vector& a)
{
return vector (x + a.x, y + a.y);
}

ostream& operator << (ostream& o, vector& a)
{
o << "(" << a.x << ", " << a.y << ")\n";
return o;
}

You have two destructor before cout because two automatic object are
destroyed (one from created new object for parameter operator+ and one
for returning new object for this operator).

Third destructor you have question about (after cout) is for automatic
object created for this cout (operator<<).

Hope this cleared something
regards Michal
Aug 5 '05 #3

"misiek3d" <mi***********@poczta.onet.pl> wrote in message
news:dc**********@nemesis.news.tpi.pl...
ra*******@gmail.com wrote:
[]
From above program what i get is that class objects a,b,c calls constructors 1,2,3 then function operator + (vector a) calls
constructor 4 but why this function corresponds to 2 destructor
functions?
Again it can be easily understood that constructors 1,2,3 has
corresponding destructors 4,5,6 then for what purpose destructor 3
called.
I am referring my output for numbering constructors and destructors.


Do You have a default constructor with 'cout<<"called constructor
"<<cnt++<<"times\n";' instruction? I don't see it in your code, but you
assume that it should print text due to construct objects a and b. I'm
sure you have not copy constructor with print instruction.
First of all your operator+ and operator<< work with *values* and not
*references*.


Yes and no, this depends!
It should looks like this:

vector& vector::operator + (vector& a)
{
return vector (x + a.x, y + a.y);
}

No, it shouldn't. You're returning a reference to locally created object,
which is a bad thing! I'd recommend to read the FAQ section 13.9
ostream& operator << (ostream& o, vector& a)
{
o << "(" << a.x << ", " << a.y << ")\n";
return o;
}


In this case it's okay because the already existing stream was passed..

[SNIP]

Cheers
Chris
Aug 5 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Murat Tasan | last post by:
i have a class and a general constructor, then i have some other constructors, each of which i'd like use the general constructor. so in the other ones, i tried something like: this =...
5
by: darkstorm | last post by:
I have a problem with constructors. Consider this: class A { public: void Fun(); private: Vector2D vec;--------(A)
8
by: aravandor | last post by:
I'm a C++ beginner coming from Java and VB.Net I know that this is an elementary question, but I'm stumped. I'm trying to make a coordinate class to store 2 Dimensional points and a Rectangle...
3
by: Daniel Diehl | last post by:
Good morning! Now I'm sitting over 2 hours on a problem calling a method in the logitech SDK DLL. Everything is working fine, but calling one method I fell in a problem. I'm not that old with C#...
5
by: James Radke | last post by:
Hello, I am using VB.NET to call an unmanaged DLL which contains some functions. When I call the DLL from a windows application, it all works fine. When I place the same code in a webform...
0
by: Tako | last post by:
I have a problem calling .NET classes with default properties from VB6. The class has a public member, this public member has a default property, from VB6 I can´t acces the default method without...
3
by: Merav Orion via .NET 247 | last post by:
I have a problem calling webservice from client side javascript. The javascript call the settimeout() method. when the user press submit button it ignore the press and keep refreshing the page. it...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
3
by: marcwentink | last post by:
Say I have a class A, and a class B that inherits from A. Now A (and B) has a virtual destructor and a virtual function F(); If I now make these statements A* ptrA = new B; ptrA->F(); delete...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.