Inheritance forward ad

This compiles fine, although I would not want to run it. However...

//class base;
//class derived;
//class derived : public base;

class base {};
class derived : public base {};

class other
{
    public:
        void func() {base1 = derived1;}
        base* base1;
        derived* derived1;
};

void main()
{
}

... moving another class over the definition of the base and the derivative, for which there is a similar thing that I have to do in the myne program, causes compilation errors.

The obvious solution is to forward the base declaration and the output shown at the top of the code, but this does not allow the base * and derivative * errors to be converted. Attempting to submit a declaration, including inheritance information, also does not work.

+5
source share
2 answers

That should work. You need to move the other up

func . , func "", .

.

class base;
class derived;
//class derived : public base;

class other
{
    public:
        void func();
        base* base1;
        derived* derived1;
};

class base {};
class derived : public base {};

void other::func() { base1 = derived1; }
+9

, . , ( / ), base1 = derived1 , / , , derived base.

, , func (. AbstractDissonance), , , , reinterpret_cast:

class base;
class derived;

class other
{
    public:
        void func() {base1 = reinterpret_cast<base*>(derived1);}
        base* base1;
        derived* derived1;
};

, ++, , / . , func (.. ), .

+4

All Articles