473,574 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1332
<ga*******@voil a.fr> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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<typena me 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_na me() << 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*******@voil a.fr> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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
3670
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 template. Example #1: B-trees While implementing a B-Tree I ended up with an (abbreviated) node structure looking something like this:
3
1731
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> <Child>Name</Child> <Child>Peter</Child>
0
1375
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 indexing was used, I could do something like child = hole * 2 + 1; but since only thing the function accepts are random access Iterators, how do I...
4
2740
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 { ... }; idiom (don't know the name of it, if there is one). The problem is that I don't want any of my classes to have public constructors. They should be...
3
3929
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. template<class Type> class base { public: base& operator/=(const base&); Type *image;
6
1572
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 B can be child to zero, one, or more A's. I just call that "A is a parent of B", and "B is a child of A". The same goes for B and C and for C and D....
1
1488
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, bool Signed> class foo { string str(); ... };
2
1904
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 should do nothing. I've come up with a solution, that compiles well with the online Comeau compiler, but not with VC++ 7.1 .
3
2201
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 "/". What do I need to change in my XSLT to get it to actually start processing my document's node other than /? Mark Input: <?xml...
0
7737
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8075
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8253
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7827
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6471
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2253
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1356
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1072
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.