Finding data type at run time to create an identical object

Hello all,

I hope this thread matches in here.

I am working with Visual C++ 6.0.

I am trying to create a vector that will be used as a buffer for an algorithm. The problem is that I want the algorithm to work on a "template" type vector.
Therefore, my buffer vector must be created at run time with the type of the vector supplied as input.

(for example, say the input vector is "vector<int> data"
the buffer should be created as type integer)

I found a way to obtain the type using type_info:

from MSDN
("The type_info::raw_name member function returns a const char* to a null-terminated string representing the decorated name of the object type."
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/express_80.asp)

ok, so now I have the data type stored in a string.
My question is how to use this string to create the correct type of buffer vector ?

Thanks !

[1193 byte] By [underSt8ment] at [2007-12-16]
# 1
Hi: unfortunately the C++ language only supports the simplest of reflection operations: you can get get the name of the type and nothing else.

So in order to be able to do what you want in C++ you will probably need to implement your own reflection classes or find a different mechanism.

JonathanCavesMSFT at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 2
Hi,
as Jonathan already said the absolutly native C++ knows only of the class name and the before when you compile with RTTI flag set.

Solution:
1. Use managed languages like C++.NET ( with Meta info you have what you want )
2. Implement an RTTI class and derive all other class from a Object class which implements RTTI functionality and overide functions like GetClassName or GetClassType.

Then use a design pattern like a class factory to create dynamically instances at runtime. I already did so in older projects. This is all like using a crook and makes much work, but it does what you want.

Bye
Martin


maddin123 at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...