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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:36:24+00:00 2026-05-23T09:36:24+00:00

This is a basic concept question. If I have a class that Derived that

  • 0

This is a basic concept question. If I have a class that Derived that inherits from Base, and I instantiate a new Derived object, can I set it’s Base object to a specific Base object of my choosing so that all calls base class methods are redirected to this particular base object?

something like this:

class Base
{
protected:
    string name;
public:
    Base(string n) { name = n}
    void doSomething(){cout << name << "\n";}
};

class Derived : public Base
{
public:
     Derived(string n) : Base(n) {}

int main()
{
    Derived* d = new Derived("original base"); //create a derived
    d->doSomething(); // prints "original base"
    Base* rB = new Base("replacement base"); // create a new base object
    ((Base*) d) = rB; // replace the base object of d with a new one (pretend code)
    d->doSomething(); // prints "replacement base"

    return 0;
}

I’m sure I made all sorts of errors in that simple code, because my skill level is low, but just for the idea.

Is this possible in C++? We can slice the derived information off of an object, so can we separate and replace the components in a chain of inheritance?

Why would I want to do this?

Consider the mixin lilies: (again, forgive syntax errors)

template <class T> class MyMixin : public T
{
public:
    MyMixin(T desiredBaseObject)
    { 
        // do something to set the desired base 
        // object of this class to desiredBaseObject.
    }
};

RandomClass1 dog(int i = 0);  
RandomClass2 cat(double i = 0.0);
MyMixin< RandomClass1 > mixin1(dog);
MyMixin< RandomClass2 > mixin2(cat);

In this case, if we could set the base object of the mixin to any desired object, we could use constructors with any parameter list in our mixin without the mixin needing to know anything about it. Also, the mixin could be used like a decorator without the need for a common interface amongst decorators.

Thanks for the answers. Since we can slice off the derived part of an object, it seems like the base and derived information lives separately. Could someone comment on this? Could we access some internal table, like the vtables I hear so much about (I don’t know anything about this type of stuff, so maybe this is not applicable), and accomplish this?

@Benoît

Could you explain why only 1 and 4 work, but 2 and 3 do not?
class Base
{
protected:
std::string name;
public:
Base(std::string n)
{
name = n;
}

    virtual void doSomething()
    {
        cout << name << "\n";
    }
};

class Derived : public Base
{
public:
    int x;
    Derived(std::string n) : Base(n)
    {
        x = 5;
    }

    void printX()
    {
        cout << "x = " << x << "\n";
        x++;
    }
};


Derived* d1 = new Derived("original 1");
d1->doSomething();
d1->printX();
Base* rb1 = new Base("new 1");
*static_cast<Base*>(d1) = *rb1;
d1->doSomething();
d1->printX();
cout << "\n\n";

Derived d2 = Derived("original 2");
d2.doSomething();
d2.printX();
Base b2 = Base("new 2");
static_cast<Base>(d2) = b2;
d2.doSomething();
d2.printX();
cout << "\n\n";

Derived d3("original 3");
d3.doSomething();
d3.printX();
Base b3("new 3");
static_cast<Base>(d3) = b3;
d3.doSomething();
d3.printX();
cout << "\n\n";

Derived d4("original 4");
d4.doSomething();
d4.printX();
Base b4("new 4");
*static_cast<Base*>(&d4) = *&b4;
d4.doSomething();
d4.printX();
cout << "\n\n";

this will print:

original 1
x = 5
new 1
x = 6

original 2
x = 5
original 2
x = 6

original 3
x = 5
original 3
x = 6

original 4
x = 5
new 4
x = 6

Why does this only work with when using a pointer?

  • 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-23T09:36:25+00:00Added an answer on May 23, 2026 at 9:36 am

    I’m not questioning why you want to do this, but it’s perfectly safe do it unless your inheritance breaks the ISA relationship (eg Derived is a restricted subset of Base, eg a Square is not a Rectangle since it is possible to resize only one dimension of a Rectangle but impossible to do so with a Square).

    *static_cast<Base*>(d) = *rB;
    

    (works also with references)

    or you can write a little function (you will find lots of functions doing this):

    template<typename T>
    T& assign(T& to, const T& from)
    {
      return to = from;
    }
    
    assign<Base>(*d, *rB);
    

    and anyway, you do this every time you overload/redefine operator=

    Derived& operator=(const Derived& other)
    {
      // prettier than the cast notation
      Base::operator=(other);
    
      // do something specific to Derived;
      this->name += " (assigned)";
    
      return *this;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So this seems pretty basic but I can't get it to work. I have
I know this maybe a basic question but I just can't seem to find
This is a really basic question but... I have some code like this var
this is a total beginner question. My question is about data that you can
This is basic stuff, but I'm somewhat unfamiliar with VBA and the Word/Access object
I hope this question is not considered too basic for this forum, but we'll
This is a fairly basic question, which for some reason, a proper solution escapes
This is a really basic question but this is the first time I've used
This is a very basic question. I'm just on my mission to learn ASP.NET
First of all this is all just concept, I have no actual programming done

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.