tags:

views:

65

answers:

2

Hello :) i would like to ask, if it's posible to make something like this:

i have base class (parent) A and three other classes (childs) B C D in class A, i have virtual functions, that's ok. but what if i need a virtual class ?

class A 
{
public:
    virtual int func1()=0;
    virtual int func2()=0;

    virtual class AB; // !!!!???
};

class B
{
public:
    int func1();
    int func2();

    class AB
    {
    public:
    ....
    };
};

classes B C D are same as class B. Now, i would like to create class instance and it should automaticly "redirect" class to instance of B or C D etc like functions.

is it possible ? i hope, you understand :) Thank you very much for answer.

A: 

At first my initial thought was to introduce a template parameter to reflect the type of class you'd want of AB but that fails to produce the type of result you'd ultimately like to see. Instead, you could just create two "base" classes, one contained within the other for the same type of effect. Then, class B would have a member of AB:: public WithinA that you could access via a pointer from A.

class A{
    //stuff
    class WithinA{};
};

class B : public A{
    //more stuff
    class WithinB : public A::WithinA{
        //stuff
    };
};
wheaties
+1  A: 

This is fundamentally impossible. A virtual function call is determined at runtime. A class changes the behaviour of the program at compile-time. You can't make a compile-time determination at runtime unless runtime and compiletime are the same time, i.e. using a JIT or other dynamic code generators. In standard C++, this is impossible.

What you CAN do is have a base class AB, with a virtual function that creates a class that is guaranteed to inherit from this base class, and then return a pointer to that.

DeadMG
That's reason, why there isn't any info about "virtual class" :) thank you for answer
wh1sp3r