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

  • SEARCH
  • Home
  • 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 7009545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:52:34+00:00 2026-05-27T21:52:34+00:00

The code is: #include <iostream> class P_Node { friend class Picture; protected: P_Node() :

  • 0

The code is:

#include <iostream>

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) {
        std::cout << "Constructor\t" << "Picture::Picture()" << "\tcalled" << std::endl;
        std::cout << "Picture p count\t" << p->use << std::endl;
    }
    Picture(const Picture& orig) : p(orig.p) {
        std::cout << "Copy Constructor\t" << "Picture::Picture(const Picture&)" << "\tcalled" << std::endl;
        std::cout << "Picture p count\t" << p->use << std::endl;
        orig.p->use++;
    }
    ~Picture() {
        std::cout << "Destructor\t" << "Picture::~Picture()" << "\tcalled" << std::endl;
        std::cout << "Picture p count before decrease\t" << p->use << std::endl;
        if(--p->use == 0) {
            std::cout << "Picture p count after decrease\t" << p->use << std::endl;
            std::cout << "Deleted" << std::endl;
            delete p;
        }
    }
    Picture& operator=(const Picture& orig) {
        std::cout << "operator=\t" << "Picture& Picture::operator=(const Picture& orig)" << "\tcalled" << std::endl;
        std::cout << "Picture p count before decrease\t" << p->use << std::endl;
        orig.p->use++;
        if(--p->use == 0) {
            std::cout << "Picture p count after decrease\t" << p->use << std::endl;
            std::cout << "Deleted" << std::endl;
            delete p;
        }
        p = orig.p;
        return *this;
    }
private:
    Picture(P_Node* p_node) : p(p_node) {
        std::cout << "Picture::Picture(P_Node* p_node)\tcalled" << std::endl;
    }
    P_Node *p;
};

class Frame_Pic : public P_Node {
    friend Picture frame(const Picture&);
private:
    Frame_Pic(const Picture& pic) : p(pic) {
        std::cout << "Frame_Pic::Frame_Pic(const Picture& orig)" << "\tcalled" << std::endl;
    }
    Picture p;
};

Picture frame(const Picture& pic) {
    return new Frame_Pic(pic);
}

int main() {
    Picture my_pic;
    Picture temp = 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 previously asked a question about memory management of this code, but after understanding the answers, I still have a problem with the destructor and the copy constructor. In my understanding, Picture temp = frame(my_pic) will call the copy constructor.

Here comes the question:

  1. Why isn’t the copy constructor called after Picture temp = frame(my_pic)
  2. and why is the destructor called?
  3. In Picture frame(const Picture& pic), will the copy constructor be called if the function is called? I believe so, because it returns a ‘Picture’ by value.
  4. If I change Picture frame(const Picture& pic) to Picture frame(Picture p) will the copy constructor called twice when the function is called?
  5. When will the copy constructor be called? Will it happen when the class is returned by a function by value? When then class is passed to a function by value?
  6. When will the destructor be called? Is it when each time a variable’s lifetime is ended? Does that mean if I pass a variable to a function by value, its destructor will be called after the functions execution?

I’m messed up with the copy constructor and the destructor right now, especially when I have a function with a return value, and some parameters all passed by values.

Also, will anyone help me to write a comment on each line of the output strings? That would be very helpful.

  • 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-27T21:52:35+00:00Added an answer on May 27, 2026 at 9:52 pm

    In answer to your questions.

    1. The copy constructor isn’t called after the statement Picture temp = frame(my_pic); because you don’t have any statements that cause any copies after that statement.

    2. The three destructors for Picture are called to destroy (in order): temp, p in the Frame_Pic pointed to by temp.p and my_pic. Your compiler has avoided generating any other temporary Picture objects.

    3. Yes, a copy constructor may be called to initialize the return value of Picture frame(const Picture& pic) but the compiler is allowed (and does in the case) to eliminate the copy and initialize the return value directly from the return expression.

    4. Yes, an additional copy constructor call may be generated if you change the parameter for frame to be passed by value but if the parameter is initialized with an expression that isn’t a glvalue referring to an existing object the argument might be initialized directly with that expression and the copy elided.

    5. A copy constructor is called whenever an object of class type is actually copied. This may be when being passed to a function or returned from a function but sometimes compilers are allowed to omit unnecessary copies in these scenarios.

    6. Yes, a destructor is called whenever an object of class type is destroyed. This is true for named variables and temporaries generated by the compiler. It is possible to end an object’s lifetime without calling a destructor, e.g. my re-using its memory for another object, but this is very much a special case.

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

Sidebar

Related Questions

Example code: #include <cstdlib> #include <iostream> using namespace std; class A { public: A(int
I have this code #include <iostream> using namespace std; class Test{ public: int a;
Given the following code: #include <iostream> using namespace std; class CRectangle { public: int
code: #include<iostream> using namespace std; template<class T, int N> class point { T coordinate[N];
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
I have the following code: #include <iostream> #include boost/shared_ptr.hpp using boost::shared_ptr; class Base {
I'm trying to compile such code: #include <iostream> using namespace std; class CPosition {
I've just come across the following code: #include <iostream> static class Foo { public:
Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
I have the following code compiled by gcc: #include <iostream> using namespace std; class

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.