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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:07:28+00:00 2026-06-09T12:07:28+00:00

My very first post here! I have been using this site for many years

  • 0

My very first post here! I have been using this site for many years as a reference and to find solutions to common problems. Unfortunately this problem I am facing is one that have not been found here yet! So here it is. I have been working on a project for some time now. My program has several thousand lines of code so I will not post it all here. Basically, I have a subclass in which I wish to change the parent class to one that has been already initialized in my code some place. I am not sure if this is possible or if it is good code practice. But I will let you guys be the judge of that! Here is the problem I am facing:

#include <stdio.h>

class Base

    public:
        int data;
        Base(int);
};

class Child : public Base
{
    public:
    Child(void);
        void run(Base*);
};

Base::Base(int var)
{
    data=var;
}


void Child::run(Base* base)
{
      this = base //I know that you can create a reference to the parent class
                    //by casting this pointer to a Base pointer, but was wondering
                   //if you can do this the other way around. 
    printf("%d\n",Base::data);
}

int main()
{
Base* base1 = new Base(5);
Base* base2 = new Base(3);
    Child one();

one.run(base1);

    delete base1;
    delete base2;
    base1=0;
    base2=0;
    return 0;
}

So if this compiles(which it does not) it would outputt something like 5 and if I change the run method parameter to base2 it should print something like 3. Is this possible? Thank you!

  • 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-06-09T12:07:29+00:00Added an answer on June 9, 2026 at 12:07 pm

    No, you cannot do things like that. This is a READ-ONLY pointer. The line

     this = base;
    

    WILL generate a syntax error.

    Base classes in C++ are static. They cannot be changed at run time.

    Although, if your classes (both the base and the child) do not have virtual functions, the follwoing WILL work:

    (Base&)*this = *base;
    

    At the same time I would discourage you to do this. The reason of discouraging is because such code would be highly vulnerable for changes. For example, if virtual method will be added to either of the classes, then the VMT will be overwritten. This may not work with multiple inheritance. In my experiments with MSVC I noticed that sometimes it changes the order of base classes in the binary layout of the class.

    In other words, this can be used if you are absolutely sure of the layout that is used by compiler. If you are sure that compiler will not change this layout in the future. If not, better avoid this.

    The right approach is to create a special method and copy in the data field by field.

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

Sidebar

Related Questions

This is my first post, but I have been using this site for a
I'm new here, and this is my very first post. I have a very
Well, this is my first post here and really enjoying the site. I have
This is my first post here although I've come here for solutions very very
this is my first post here on stackoverflow and am very impressed by the
This is my first post, thx, you've been very helpful. But I'm stuck. I
So this was one of my very first questions here, but I have a
been following posts on this site for a while and find them very helpful.
This is my first post, have been a lurker for a long time, so
Ok this is my first post on Stack Overflow I have been reading for

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.