Are array of pointers to different types possible in c++? with example please)
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);
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).
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.
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.