Reputation: 781
I tried the following code (modified from learncpp.com)
#include <iostream>
#include <string>
using namespace std;
class Point2D
{
private:
int m_nX;
int m_nY;
public:
// A default constructor
Point2D()
: m_nX(0), m_nY(0)
{
}
// A specific constructor
Point2D(int nX, int nY)
: m_nX(nX), m_nY(nY)
{
cout<<"Point is created"<<endl;
}
~Point2D(){cout<<"Point is destroyed"<<endl;}
// An overloaded output operator
friend std::ostream& operator<<(std::ostream& out, const Point2D &cPoint)
{
out << "(" << cPoint.GetX() << ", " << cPoint.GetY() << ")";
return out;
}
// Access functions
void SetPoint(int nX, int nY)
{
m_nX = nX;
m_nY = nY;
}
int GetX() const { return m_nX; }
int GetY() const { return m_nY; }
};
class Creature
{
private:
std::string m_strName;
Point2D m_cLocation;
// We don't want people to create Creatures with no name or location
// so our default constructor is private
Creature() { }
public:
Creature(std::string strName, const Point2D &cLocation)
: m_strName(strName), m_cLocation(cLocation)
{
cout<<"Creature is created"<<endl;
}
~Creature(){cout<<"Creature is destroyed"<<endl;}
friend std::ostream& operator<<(std::ostream& out, const Creature &cCreature)
{
out << cCreature.m_strName.c_str() << " is at " << cCreature.m_cLocation;
return out;
}
void MoveTo(int nX, int nY)
{
m_cLocation.SetPoint(nX, nY);
}
};
int main()
{
using namespace std;
cout << "Enter a name for your creature: ";
std::string cName;
cin >> cName;
Creature cCreature(cName, Point2D(4, 7));
while (1)
{
cout << cCreature << endl;
cout << "Enter new X location for creature (-1 to quit): ";
int nX=0;
cin >> nX;
if (nX == -1)
break;
cout << "Enter new Y location for creature (-1 to quit): ";
int nY=0;
cin >> nY;
if (nY == -1)
break;
cCreature.MoveTo(nX, nY);
}
return 0;
}
When I run the program I get the following:
Enter a name for your creature: gabar
Point is created
Creature is created
Point is destroyed
gabar is at: (4,7)
Enter new X location for creature (-1 to quit): 2
Enter new Y location for creature (-1 to quit): 3
gabar is at: (2,3)
Enter new X location for creature (-1 to quit): -1
Creature is destroyed
Point is destroyed
I have two questions:
Why is the first "Point is destroyed" called? Why are there two prompts for "Point is destroyed" when there is only one "Point is created" prompt.
thanks
Upvotes: 1
Views: 128
Reputation: 133567
Because you are just constructing one Point2D
with your normal constructor, and this happens in your main method.
The second Point2D
, which is the member variable m_cLocation
is not constructed but initialized through the copy constructor, which is implicitely defined by the compiler in your case, and not tracked by your console prints. This constructor has the signature Point2D (const Point2D& other)
.
The first point is destroyed right after being used to initialize the member variable because it's an rvalue and it's not needed anymore after its use.
Upvotes: 1
Reputation: 310950
In this statement
Creature cCreature(cName, Point2D(4, 7));
a temporary object Point2D(4, 7)
is created as the second argument. After finishing the statement this object is deleted.
In your program there are two objects of type Point. The first one is the temporary object mentioned above and the second object is the member of class Creature. After the temporary object was copied in the member object by the constructor cCreature it was deleted.
Upvotes: 2
Reputation: 5187
I don't know what you're using to track constructors and destructors, but I suspect it is not detecting copy constructors.
This line creates the first Point2D
:
Creature cCreature(cName, Point2D(4, 7));
The constructor for Creature
takes it by reference, but then assigns it to m_cLocation
, which copies it (invoking the Point2D
copy constructor that I suspect isn't being traced).
The first Point2D
, which was transient and never stored, is then destroyed. m_cLocation
is destroyed at the end.
Upvotes: 5