Jump to content
  • Advertisement
Sign in to follow this  
Decrius

[C++] Heterogeneous arrays storing the type?

This topic is 3690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Yes, another hetero-array question ;) Suppose I have an array of type "pointer to base class" and I add "pointer to child class" instances. So far, one can call any function of a child class if it's specified as a virtual in the base class. However, I need to cast the "pointer to base class" to a "pointer to child class" in order to call functions that are _not_ specified as virtuals in the base class. Since the child class, beside a few required basic functions (which are specified virtual in the base class), the child class can have an unlimited number of other functions one should be able to call when one has the "pointer to the base class". The problem, you have to know which child class is stored in order to be sure what function calls you can do. I thought of something like this, but it has a few flaws:
class _Item // base class, to allow heterogeneous arrays (of type _Item *)
{
};

////////////////////////////////////////////////////////////////

template <class T>       // the class used
class Item: public _Item // derived class, allows the use of any class (using templates)
{
public:
    T *pointer;

    Item(T *poiner) :
    pointer(pointer)
    {
    }
};

class T
{
public:
    void f()
    {
        std::cout << "Invoked\n";
    }
};

int main()
{
    T *t = new T;

    _Item *items[1];
    items[0] = new Item<T>(t);
    //items[0].pointer->f();

    delete items[0];
    delete t;
    return 0;
}
This way the user would set the type as template parameter when it adds the class to the array and it will remember the type. However, pointer is not specified in _Item, so I can never use that in the code. Ultimately, I would like any call, any operators, function calls etc, be passed onto the template type T immediately... So, in other words, can I remember a type and not require the user to cast to this type when he wants to use child class functions? Or is this not what I should want to do?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Decrius
However, I need to cast the "pointer to base class" to a "pointer to child class" in order to call functions that are _not_ specified as virtuals in the base class.

That's not the idea of polymorphism. Why do you think you need that?

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Quote:
Original post by Decrius
However, I need to cast the "pointer to base class" to a "pointer to child class" in order to call functions that are _not_ specified as virtuals in the base class.

That's not the idea of polymorphism. Why do you think you need that?


Well, I've made a signal and slot system, where connected functions, when a signal is emitted, will be called. These functions must have 1 parameter: a "pointer to base class". This pointer is passed and points to the child class (child of the base class) who emitted it.

So, in a child class called "Child", it will only ever pass a Child * ('this', for the child). And Child2 will only ever pass Child2 *.

So, I could use templates and initialise the Signals class in the Child instead of the base class...yeah, that might work...let me try :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!