views:

439

answers:

6

Are array of pointers to different types possible in c++? with example please)

+4  A: 

... if you use a void *, it's easy ;-)

jldupont
can you give little example?))
SomeUser
and lead to potentially explosive situations!
CookieOfFortune
char *c_string = "Hello World";MyCPlusPlusClass *obj = new MyCPlusPlusClass();void *SomeArray[] = {c_string, obj};If course, you'd damn well better have a way of remembering what's what. :-)
asveikau
+10  A: 

Usually if you want to have a collection of different "types" of pointers, you implement it in a way where they derive off a base class/interface and store a pointer to that base. Then through polymorphism you can have them behave as different types.

class Base
{
public:
    virtual void doSomething() = 0;
};

class A : public Base
{
    void doSomething() { cout << "A\n"; } 
};

class B : public Base
{
    void doSomething() { cout << "B\n"; } 
};

std::vector<Base*> pointers;
pointers.push_back(new A);
pointers.push_back(new B);
RC
I like this answer. I would add that you can also query which type is which dynamically, using dynamic_cast... Though some would argue that's a bad thing. :-)
asveikau
@asveikau: A `dynamic_cast` is, basically, a `switch` over types. And Switching over types is just a sign of (irrational) fear of polymorphism. When you're tempted to do that, use virtual functions instead.
sbi
I vote dynamic_cast being a bad thing :-)
stu
+1  A: 

Yes; just cast your pointers in the array to whatever type you want them to refer to.

Alternately, you could make your array an array of a union (with the union elements being the differing pointer types).

McWafflestix
+8  A: 

An array of pointers to void has already been mentioned. If you want to make it practical and useful, consider using an array (or, better, vector) of boost::any.

Jerry Coffin
+1  A: 

C++ is C with more stuff. So if you want to do it the C way, as above you just make an array of void pointers

void *ary[10];
ary[0] = new int();
ary[1] = new float();

DA.

If you want to do things the object oriented way, then you want to use a collection, and have all the things you going to be adding to the collection derive from the same base object class that can be added to the collection. In java this is "Object", C++ has no base object built in, but any collection library you use will have such a thing that you can subclass.

stu
Given what you showed, how in the world are you to find out what types those pointers actually point to?
sbi
well, firstly, that wasn't the question, but to answer, there's all sorts of ways.1) you can have a parallel array with the type encoded in it.2) you can make the array elements structures of a type variable and a union with all the different kinds of things you might want to put in there.There's 2 just off the top of my head. I'm sure somebody else can come up with slicker alternatives.
stu
@sbi: and you can also use RTTI.
kriss
@kriss: No, you can't. RTTI only works within polymorphic class hierarchies. Given a `void*`, the runtime system gives you nothing to find out what it points to.
sbi
@sbi: you are right. If he want's to use RTTI the OP should put all his structures in a common class hierarchy, however the base class can still be quite empty. Well, that's not much better than using a union plus a discriminating enum type.
kriss
in fact, it's basically the same thing. :-) Except there's more C++ gunky overhead. :-))
stu
+3  A: 
Loadmaster