1

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!

4

3 回答 3

1

You cannot do precisely what you want, but you may be able to do something similar. If the parent class has a copy constructor, which lets you create an instance which is a copy of another instance, then you can use that copy constructor to create your parent object while your child object is being constructed:

class Child : public Base
{
    public:
    Child(const Base &b) : Base(b) { ...}

Now you have a Child object whose Base parts are identical to those of another Base object.

于 2012-07-28T20:40:59.437 回答
1

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.

于 2012-07-28T20:42:47.857 回答
0

Consider this to be const. When you obtain a reference to the base class, you're simple obtaining a reference to some subset (portion) of the subclass; the base class instance is tightly bound to the class-as-a-whole (subclass instance in this case). You can direct how that base class is initialized (see Ernest's response), but you cannot break that link.

Usually the memory layout of a class-with-base-class is something like:

[ ... | base-class | ... ... .. ]

It's literally embedded, as opposed to existing independently (the following is not normal, I'd even go so far as to state it never happens - but that's an internal compiler choice):

[ ... | reference to-base-class | ... ... .. ] [ independent base-class instance ]

于 2012-07-28T20:44:56.533 回答