|
Short answer is no. One of the worst possible ways to learn a new programming language is to try to replicate features from another language. And you're trying to learn C++ by replicating a capability of Objective C which C++ is not really designed to support. Similarly, trying to learn Objective C by replicating something like C++ templates would be a poor way to learn Objective C.
That said, there are ways. But they require moderately advanced knowledge of C++, and some other programming methodologies, which you don't yet have.
ONLY OBSESSIVE FOOLS WILL READ ON!!!!
You're talking about a generic container, with different types of elements able to be placed into one container. Objective C supports that more easily, as all types are derived from a base type (id). In C++, there is no universal base type.
The closest you can come would be an array that works with a specific set of types. If those types are only basic types (int, float, pointers, etc) you can employ a union to hold the data, but you also need some other information (eg an enumerated type) to keep track of what data type is actually stored in the union. If the types you want to store include C++ classes, you could do do some hackery with void pointers, and would still need to carry around information about the type of object being stored. And you would need to recompile every time you want to introduce a new type of object into your container (in effect, because a void pointer must be converted into a pointer to the actual type before the actual object can be used).
You will find doing the above non-trivial, because C++ isn't really intended to be used for such things: one of the premises of C++ is that a function that receives a generic void pointer KNOWS what conversions are needed before that void pointer can be used.
There are some more advanced techniques to do the sort of thing you want (eg a common base class for all types you wish to store in the array; defining such a base class is non-trivial and also gives code maintenance nightmares) or introspection techniques associated with distributed object frameworks (CORBA, DCOM, etc). I would recommend you learn more of the basics of C++ before trying to use such things.
|