472,355 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,355 software developers and data experts.

Finding template argument of child

I have the following situation:

class A
{
public:
virtual ~A() {}
};

template < typename T >
class B : public A
{
};

The problem is to find the template argument T when a function receives
only a pointer A* (and we know for sure that the A* indeed points to a
B<T>*).

One way is to dynamic_cast to a bunch of know B<T>, but this is kind of
heavy and works only for types that are explicitely taken into account.
Is there a way to retrieve type T for any T, and say print it out?

Thanks

Bolin

Jul 23 '05 #1
2 1264
<ga*******@voila.fr> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
template < typename T >
class B : public A {};

The problem is to find the template argument T when a function receives
only a pointer A* (and we know for sure that the A* indeed points to a
B<T>*).


In which context do you want to find the T?
If it is just to "say, print", then the easiest way is to define a
non-template intermediate base class which implements the necessary
interface:

#include <iostream>
#include <typeinfo>
#include <string>

struct A
{
virtual ~A() {}
};

struct B_base : A
{
std::string get_T_name() const { return vget_T_name(); }
private:
virtual std::string vget_T_name() const =0;
};

template<typename T>
struct B : B_base
{
private:
std::string vget_T_name() const { return typeid(T).name(); }
};

int main()
{
B<int> b_int;
A& a= b_int;
B_base& b_base= dynamic_cast<b_base&>( a );
std::cout << b_base.get_T_name() << std::endl;
}
Hope it helps,
Ruslan Abdikeev.
Jul 23 '05 #2
How about:
class A

{

public:

A(){}

virtual ~A() {}

virtual const type_info & getTypeInfo() const {return typeid(A); }

};

template <class T>

class B : public A

{

public:
virtual const type_info & getTypeInfo() const {return typeid(T); }

};

int main(int argc, char **argv)

{

A anA;

A* pA = new B<int>;

std::cout << anA.getTypeInfo().name() << std::endl;

std::cout << pA->getTypeInfo().name() << std::endl;

return 0;

}

regards,
Aiden

<ga*******@voila.fr> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I have the following situation:

class A
{
public:
virtual ~A() {}
};

template < typename T >
class B : public A
{
};

The problem is to find the template argument T when a function receives
only a pointer A* (and we know for sure that the A* indeed points to a
B<T>*).

One way is to dynamic_cast to a bunch of know B<T>, but this is kind of
heavy and works only for types that are explicitely taken into account.
Is there a way to retrieve type T for any T, and say print it out?

Thanks

Bolin


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: SpeedBump | last post by:
This is mostly just another "gee it would be nice if it had X" post. Recently I have come across two separate problems which both exhibit the need for a way to self reference when instantiating a...
3
by: Jamie Green | last post by:
Using MSXML3.0, with the Dom SelectionLanguage set to Xpath I am trying to query the following document <Root> <Child>Name</Child> <Child>John</Child> <Child>Smith</Child> <Child>23</Child>...
0
by: CoolPint | last post by:
I am trying to write a generic heapsort (of course as a self-exercise) with Iterator interface: something like blow.... But I got into trouble finding out the Iterator to the Child node. If...
4
by: Grey Plastic | last post by:
I have several classes that all keep track of static data. However, the manner that they keep track of static data is identical, and so I'm using the template<class Child> class Parent { ... };...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
6
by: Hendrik Schober | last post by:
Hi, I have a problem with extending some existing code. In a simplified form, the problem looks like this: I have four types, A, B, C, and D. Each A refers to zero, one, or more B's and each...
1
by: Joseph Turian | last post by:
How can I specialize the value of only one template parameter? Here's the fragment of code I'd like to get working: typedef enum {START, PARENT, CHILD, END} locator_ty; template <locator_ty L,...
2
by: Imre | last post by:
Hi I'm trying to write a function template called CallF(), that has a type argument called T. It has one argument, a T*. If T has a member function F(), then CallF() should call it, otherwise it...
3
by: MarkMYoung | last post by:
I'm trying to transform an ugly Lotus Notes database into a condensed XML file for loading into another product that only needs ~25% of the data. For some reason, my template matches only work for...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.