Here is the code first, it comes from ‘Ruminations on C++’ chapter 10
// TestCode.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class P_Node
{
friend class Picture;
protected:
P_Node() : use(1)
{
}
virtual ~P_Node()
{
}
private:
int use;
};
class Picture
{
friend Picture frame(const Picture&);
public:
Picture() : p(new P_Node)
{
cout << "Constructor\t" << "Picture::Picture()" << "\tcalled" << endl;
cout << "Picture p count\t" << p->use << endl;
}
Picture(const Picture& orig) : p(orig.p)
{
cout << "Copy Constructor\t" << "Picture::Picture(const Picture&)" << "\tcalled" << endl;
cout << "Picture p count\t" << p->use << endl;
orig.p->use++;
}
~Picture()
{
cout << "Destructor\t" << "Picture::~Picture()" << "\tcalled" << endl;
cout << "Picture p count before decrease\t" << p->use << endl;
if(--p->use == 0)
{
cout << "Picture p count after decrease\t" << p->use << endl;
cout << "Deleted" << endl;
delete p;
}
}
Picture& operator=(const Picture& orig)
{
cout << "operator=\t" << "Picture& Picture::operator=(const Picture& orig)" << "\tcalled" << endl;
cout << "Picture p count before decrease\t" << p->use << endl;
orig.p->use++;
if(--p->use == 0)
{
cout << "Picture p count after decrease\t" << p->use << endl;
delete p;
}
p = orig.p;
return *this;
}
private:
Picture(P_Node* p_node) : p(p_node)
{
// Why not p_node->use++?
cout << "Picture::Picture(P_Node* p_node)\tcalled" << endl;
}
P_Node *p;
};
class Frame_Pic : public P_Node
{
friend Picture frame(const Picture&);
private:
Frame_Pic(const Picture& pic) : p(pic)
{
cout << "Frame_Pic::Frame_Pic(const Picture& orig)" << "\tcalled" << endl;
}
Picture p;
};
Picture frame(const Picture& pic)
{
return new Frame_Pic(pic);
}
int main(int argc, char* argv[])
{
Picture my_pic;
frame(my_pic);
return 0;
}
The result is:
Constructor Picture::Picture() called Picture p count 1 Copy Constructor Picture::Picture(const Picture&) called Picture p count 1 Frame_Pic::Frame_Pic(const Picture& orig) called Picture::Picture(P_Node* p_node) called Destructor Picture::~Picture() called Picture p count before decrease 1 Picture p count after decrease 0 Deleted Destructor Picture::~Picture() called Picture p count before decrease 2 Destructor Picture::~Picture() called Picture p count before decrease 1 Picture p count after decrease 0 Deleted
I have two questions about this code:
- Why is the copy constructor called before
Frame_Pic‘s Constructor? In my mind, the copy constructor is called becauseframe(my_pic)is returning aPictureby value. But that should be called afterFrame_Pic‘s Constructor. - In
Picture::Picture(P_Node* p_node), why not increase the use count? isn’t this creating a newPicture?
Thanks for any help.
I’m using VC6 under Windows XP.
Because the
pmember is being copy-constructed in the initialization list of Frame_pic’s constructor. The initialization list runs before the constructor’s body is entered.frame()is declared to return aPictureinstance by value, but it is coded to return aFrame_pic*instead.Frame_picderives fromP_node, andPicturehas a constructor that accepts aP_node*, and that constructor is accessible toframe()so the compiler allows it.The use count is on
P_node, notPicture. ThePicturethatframe()returns owns theFrame_picthatframe()creates, whose use count member is already 1 by theFrame_picconstructor. That is why thatPictureconstructor does not increment the use count – it is already at the correct value.The
Frame_piccontains its ownPicturethat is copy-constructed from anotherPicture, so thatPictureconstructor needs to increment the use count of the originalPicture.