Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7000419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:40:06+00:00 2026-05-27T20:40:06+00:00

Here is the code first, it comes from ‘Ruminations on C++’ chapter 10 //

  • 0

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:

  1. Why is the copy constructor called before Frame_Pic‘s Constructor? In my mind, the copy constructor is called because frame(my_pic) is returning a Picture by value. But that should be called after Frame_Pic‘s Constructor.
  2. In Picture::Picture(P_Node* p_node), why not increase the use count? isn’t this creating a new Picture?

Thanks for any help.

I’m using VC6 under Windows XP.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:40:07+00:00Added an answer on May 27, 2026 at 8:40 pm

    1, Why is the Copy Constructor called before Frame_Pic’s Constructor?

    Because the p member is being copy-constructed in the initialization list of Frame_pic’s constructor. The initialization list runs before the constructor’s body is entered.

    In my mind, the Copy Constructor is called because frame(my_pic) is returning a Picture by value. But that should be called after Frame_Pic’s Constructor.

    frame() is declared to return a Picture instance by value, but it is coded to return a Frame_pic* instead. Frame_pic derives from P_node, and Picture has a constructor that accepts a P_node*, and that constructor is accessible to frame() so the compiler allows it.

    2, In Picture::Picture(P_Node* p_node), why not increase the use count? isn’t this creating a new Picture?

    The use count is on P_node, not Picture. The Picture that frame() returns owns the Frame_pic that frame() creates, whose use count member is already 1 by the Frame_pic constructor. That is why that Picture constructor does not increment the use count – it is already at the correct value.

    The Frame_pic contains its own Picture that is copy-constructed from another Picture, so that Picture constructor needs to increment the use count of the original Picture.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's some code from an FTP application I'm working on. The first method comes
first post here, and probably an easy one. I've got the code from Processing's
here's the main code at first I thought is was the message box but
Let me first put the code snippets here. I am just using the ASP.NET
First off here is the code! <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
Here is the code: string str; cin>>str; cout<<first input:<<str<<endl; getline(cin, str); cout<<line input:<<str<<endl; The
So, here's my question: Why won't the code in the first snippet work when
Here is the first part of my controller code: public class ControlMController : Controller
First, here is the C# code and the disassembled IL: public class Program<T> {
First, to make my job explaining a bit easier, here's some of my code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.