Passing the class type to a method???

Started by
8 comments, last by TeeCee 21 years, 11 months ago
Hi all, I was wondering if anyone out there could help me with a problem I have - I need to pass the actual type of an object (i.e. its class) to a method, and I am unsure how to do this. I would be v. grateful for any advice/ideas. Thanks, Tony
Advertisement
Your problem is a little vague, but there is a number of ways to do this. You can use templates, typeid or even virtual functions to pass in type information. Look into these subjects or post a more detailed question. It all depends on what you want to do with that type.

Cédric
Hi cedricl,
thanks for the reply. To be more specific:
I have a templated ''terrain'' class (templated because there are so many vertex types that could be used for the terrain height map data).
I then have a Quadtree class which will create vertex buffers by taking portions of data from the terrain object. I therefore need to find a way to ascertain the type of the terrain object''s templated type (vertex type).
I don''t want to make the whole Quadtree class templated - I just need to discover the (templated) vertex type being used in the Terrain class at that point in time!
Sorry if this sounds confusing and long winded, and thanks again for any replies,
Tony.
If I understand you correctly then one solution, as stated above, is to use RTTI via typeid. The problem with RTTI is it adds a little overhead to your code. If the tradeoff is what you want, then go for it.

Your other option would be to write a typeid mechanism into your template-d terrain class. When you load your "level" in, every type terrain has it''s own typeid (an int, enum, etc..). Then add an accessor so you can retrieve the type (int getType(); ).

If you are using reinterpret_cast or dynamic_cast in your code then just use the RTTI typeid stuff - you are already compiling in RTTI overhead anyway. Otherwise the second technique may be what you are looking for.

L8r
Hi AP,
thanks for the reply. I''ve probably got myself into a situation because of bad program design etc. I''ll try and clarify my situation a little better.
Each Quadtree leaf will have a vertex buffer created for it during setup:
"CStaticVB* mpVertexBuffer;"
Now, CStaticVB is itself a templated class, capable of storing different vertex types. My Terrain class (itself templated) will have created one huge CStaticVB object holding loads of vertices.
I need to create lots of smaller CStaticVB vertex buffers (templated) within the Quadtree class, but I don''t really want to make the whole Quadtree class templated.
Therefore, I wondered if I could just get access to the (templated) type that the Terrain object is using, just so I can create smaller vertex buffers of the create type within the Quadtree class.
Phew - sorry again for the long winded reply - I''m probably still not expalining myself very well!
Any more advice would be greatly appreciated.
Thanks again,
Tony.
quote:Original post by TeeCee

I need to create lots of smaller CStaticVB vertex buffers (templated) within the Quadtree class, but I don''t really want to make the whole Quadtree class templated.

Why not? IMHO it would be, by far, the most simple solution.
Hi Dactylos, thanks for the reply.
Is there any performance hit when a class is made templated?
I will not be creating any vertex buffers on the fly, just as a pre-processing step - so perhaps there is no hit. But I just wanted to be sure.
Also, I still wanted to know if there was any other way of passing the type instead, just out of pure curiosity/bloody-mindedness!
Thanks,
Tony.
quote:Original post by TeeCee
Is there any performance hit when a class is made templated?


Templates have no run-time overhead.
Instead, they generate more code (as many versions of the function as needed).

Templates are a compile-time-only feature.
They have nothing to do with RTTI.

The template parameter is filled with what the compiler actually sees, not the dynamic type of the object during execution.

template<typename T> foo(T bar) {...};class A;class B : public A;A* bar = new B;foo( bar ); // generates foo<A*> not foo<B*> 


[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
template the allocator. (same as the above is recommending)


Hi all,
thanks for the replies. I have now made my Quadtree class templated!
Tony.

This topic is closed to new replies.

Advertisement