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 6708403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:45:19+00:00 2026-05-26T07:45:19+00:00

This is a follow up of this question. I need to implement a conversion

  • 0

This is a follow up of this question.

I need to implement a conversion operation to pass responsibility for a member object from one object to another. The conversion operation takes place in a class hierarchy. There is a Base class and two deriving classes, say Child1 and Child2. In the Base class, there is a dynamically created object I need to pass from Child1 to Child2 (along with responsibility) while the conversion happens and not let Child1‘s destructor destroy it. I’ve written a simple example to illustrate what I’m trying to achieve:

#include <iostream>
using namespace std;

class Base {
public:
    Base() {
        p_int = new int; *p_int = 0;
        cout << "In Base constructor, reserving memory." << endl;
    }
    Base(const Base& other) : p_int(other.p_int), a(other.a) {
        cout << "In Base copy-constructor." << endl;
    }
    virtual ~Base() { delete p_int; p_int = NULL; cout << "Freeing memory." << endl; }

    void setpInt(int val) { *p_int = val; }
    void setInt(int val) { a = val; }
    virtual void print() {
        cout << "Base: ";
        cout << (long)p_int << ":" << *p_int << " " << a << endl;
    }
protected:
    int* p_int;
    int a;
};

class Child1 : public Base {
public:
    Child1() : Base() {};
    Child1(const Base& base) : Base(base) {}

    void print() {
        cout << "Child1: ";
        cout << (long)p_int << ":" << *p_int << " " << a << endl;
    }
};

class Child2 : public Base {
public:
    Child2() : Base() {};
    Child2(const Base& base) : Base(base) {}

    void print() {
        cout << "Child2: ";
        cout << (long)p_int << ":" << *p_int << " " << a << endl;
    }
};

int main() {
    Child1* c1 = new Child1();
    c1->setpInt(3);
    c1->setInt(2);
    c1->print();

    Child2* c2 = new Child2(*c1);
    c2->print();

    delete c1;      //Obviously c1's destructor is called here.
    c2->print();

    delete c2;

    return 0;
}

And the outcome is:

In Base constructor, reserving memory.
Child1: 158711832:3 2
In Base copy-constructor.
Child2: 158711832:3 2
Freeing memory.
Child2: 158711832:0 2
Freeing memory.

Is there a way to do what I’m trying to do in a clean way? I can’t copy-construct p_int because it’s very heavy. The project is an embedded, AVR-project, so smart-pointers or boost library might not be available (I don’t know, though) – I just recall this might be a solution, but I’ve never used them yet.

  • 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-26T07:45:20+00:00Added an answer on May 26, 2026 at 7:45 am

    I think you need to look at reference-counted objects. Essentially, the object itself tracks it’s usage itself, and instead of storing a raw pointer to the object, you use a reference-counted pointer.

    Scott Meyers talks about this here:

    http://www.aristeia.com/BookErrata/M29Source.html

    As you’re on an embedded system, I’m not sure you can use boost::shared_ptr<> but there’s nothing stopping you implementing this as Meyers outlines.

    So, instead of having a raw pointer to the object in question in the base class, have a shared pointer/reference-counted pointer that will prevent the deletion of the object once it gets copied in to Child2. By the construction of Child2, it will have 2 references so won’t die when Child1 gets deleted. It will, however, when Child2 is deleted.

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

Sidebar

Related Questions

This is a follow up question from my previous one found here I need
This is a follow-on question from the one I asked here . Can constraints
This is a follow on question to How do I delete 1 file from
This is a follow-up question to this one . Take a look at these
This is a follow-up question to ASP.NET How to pass container value as javascript
This is a follow on question from my previously answered question here: Reading characters
(This is sort of a follow up question to one I asked before )
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
This is a follow-on question to the How do you use ssh in a
This is a follow up question to This Question . I like (and understand)

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.