Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic creation of object at run time

Status
Not open for further replies.

underSt8ment

Programmer
Jul 25, 2005
4
0
0
CA
Hello all,

I hope this thread matches in here.

I'm working in 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."

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 !
 
You can't do that.

You can't generate and compile a template class at runtime.
 
chipper,
thank you for the quick response.

I hope my question is clear,
I don't need to create a new class,
I want to define the type of the vector that will be created at run time.

for example:
template <class T>

MyClass::WorkItOut<T>(vector<T> input, int size);
{
string datatype = type_info::raw_name(input.front(); );
// input.front() will return a refernce to the first
// vector element, then raw_name stores the unknown
// datatype of the reference in the string
//============================
// this is where my problem is:
vector<datatype> MyBuffer(size);
//============================
...
}

I want the buffer vector to inherite the type from the input data.

Thanks !

P.S. I'm still in concept stage so this code was written right now (not tested yet)
 
Code:
MyClass::WorkItOut<T>(vector<T> input, int size);
   {
        vector<T> MyBuffer(size);
   }
 
I don't need to create a new class,
I want to define the type of the vector that will be created at run time.
But those two things are synonymous.

[tt]vector[/tt] is not a class, but a class tempate. It's a "class cookie cutter."

[tt]vector<int>[/tt] is a class. Specifically, it's a template class. It's a class that was defined based on a class template. This class has to be generated at compile time.

You simply cannot generate the class at run time.


[tt]typeinfo[/tt] is used for a completely different purpose. It is used to obtain run-time type info, which isn't even the same thing as static type info. A type can be declared as one thing, but have a different run-time type.

[tt]typeinfo[/tt] and any string you get from it is completely unrelated to anything you might do with templates.


Many template classes contain typedefs for related types. For example, STL containers (like vector) have a [tt]value_type[/tt] typedef, which you can use to obtain the type of an object contained within the container.

These are accessible at compile time, and you can use them to declare objects of those types, or to use as template parameters.


Anyway, anything you do with templates needs to happen at compile time. Period.
 
Hi chipper, cpjust,

cpjust, yeah... I can see how to implement it.. but it will require a declaration in the client code that defines the type of my template class, a little messy. Moreover, it makes it necessary to create another instance of my class for each different type of vector.

chipper,
I think I get your point, I dig in a little deeper and it looks like type_info can't really help in this case.

Regarding the STL value_type typedef option. I don't really know it, but I don't have access to the container.
you know what they say... if she's (the function) not your friend don't let her touch your private parts.
so if value_type is private, I can't access it in my class.
but since it is new to me, let me take a look at the concept of value_type in STL containers first.

worst case, I'll go for option B and increase the size of the vector, using the extra space as my buffer.

Thanks !
 
Why wouldn't the [tt]value_type[/tt] typedef and its friends be public? Their entire point is to be used by other code; they have to be public. Making them private would defeat their purpose.

Consider this:
Code:
// Swap the initial elements of two containers
template <class Cont>
void swap_initial_elements (Cont &c1, Cont &c2) {
    Container::value_type tmp = c.front();
    c1.front() = c2.front();
    c2.front() = tmp;
}
How would you declare [tt]tmp[/tt] if you didn't have a [tt]value_type[/tt] typedef that was publicly accessible?

If it were private, the STL wouldn't specify it because you couldn't use it.

If you weren't supposed to use it, I wouldn't even know about it to suggest it.


cpjust, yeah... I can see how to implement it.. but it will require a declaration in the client code that defines the type of my template class, a little messy. Moreover, it makes it necessary to create another instance of my class for each different type of vector.
But, as I said, the compiler already creates a different class for each type of vector.

If you use them correctly, your client doesn't have to make any messy declarations.

I think you're misunderstanding templates badly. I suggest you read a chapter on templates in a C++ textbook.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top