class Tool { };
class Hammer : public Tool { };
void f(Tool* obj)
{
cout << typeid(*obj).name() << endl;
}
...
void main(void)
{
Tool a;
Hammer b;
f(&a);
f(&b);
}
If you ask me that should output:
class Tool
class Hammer
Am I getting it all wrong? Cause it really turns out like this:
class Tool
class Tool
Bare with me, it''s my first time working with this RTTI thing
Quick replies/help would be appriciated!
"Paranoia is the belief in a hidden order behind the visible." - Anonymous
RTTI problems
July 02, 2000 02:07 PM
what happens if you do... ?
class Tool { };class Hammer : public Tool { };void f(const type_info& ti){ cout << ti.name() << endl;}...void main(void){ Tool a; Hammer b; f(typeid(a)); f(typeid(b));}
Works fine so far Thanks!
I''ll try implementing it in my actual code now...I''m praying for it to work there too
"Paranoia is the belief in a hidden order behind the visible." - Anonymous
I''ll try implementing it in my actual code now...I''m praying for it to work there too
"Paranoia is the belief in a hidden order behind the visible." - Anonymous
July 02, 2000 02:13 PM
Actually, I see what your problem is. typeid() only distinguishes between polymorphic classes -- ie, Tool needs to have at least one virtual function on it. This works:
[source
class Tool { virtual void Placeholder() {} };
class Hammer : public Tool { };
void f(Tool* obj)
{
cout << typeid(*obj).name() << endl;
}
void main(void)
{
Tool a;
Hammer b;
f(&a);
f(&b);
}
[/source]
[source
class Tool { virtual void Placeholder() {} };
class Hammer : public Tool { };
void f(Tool* obj)
{
cout << typeid(*obj).name() << endl;
}
void main(void)
{
Tool a;
Hammer b;
f(&a);
f(&b);
}
[/source]
July 02, 2000 02:15 PM
Well, the first alternative I posted may work, but it won''t be very useful, because you''re calling typeid() directly on the appropriate pointer. I imagine in your code you won''t know if something is specifically a Hammer* (that''s the whole point of RTTI), and you''ll onl have a Tool*. But like I said as long as Tool has a virtual function what you originall did should work.
Anonymous Professional
Anonymous Professional
Still doesn't work. My class already has several virtual functions. I get the names of two of my classes correctly, class CWindow and class CControl, but when I try it on a class derived from the CControl class it just outputs "class CControl"
Any more anonymous professional advice?
"Paranoia is the belief in a hidden order behind the visible." - Anonymous
Edited by - Staffan on July 2, 2000 3:29:27 PM
class IWindow { ... };class CWindow : public IWindow { ... };class CControl : public IWindow { ... };class Test : public CControl { ... };
Any more anonymous professional advice?
"Paranoia is the belief in a hidden order behind the visible." - Anonymous
Edited by - Staffan on July 2, 2000 3:29:27 PM
July 02, 2000 02:30 PM
Hmm, I don''t know. This code works perfectly for me in VC++:
I get:
class Tool
class Hammer
class Thingy
// test.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <stdio.h>#include <ostream.h>#include <iostream.h>#include <typeinfo.h>class Tool { virtual void blah() {} };class Hammer : public Tool { };class Thingy : public Hammer { };void f(Tool* obj){ cout << typeid(*obj).name() << endl;}void main(void){ Tool a; Hammer b; Thingy c; f(&a); f(&b); f(&c);}
I get:
class Tool
class Hammer
class Thingy
Hmm, it works fine for me too. With my CWindow, CControl ... classes too. The problem must lie elsewhere. I''ll keep searching...
"Paranoia is the belief in a hidden order behind the visible." - Anonymous
"Paranoia is the belief in a hidden order behind the visible." - Anonymous
Okay, I found the problem. Don''t make me tell you what it was, I''ll be to embarrassed . Anyways, how would I go about doing something like:
This obviously doesn''t work as one could wish it would. You probably guessed it already, it prints:
class Tool *
class Tool *
class Tool *
"Paranoia is the belief in a hidden order behind the visible." - Anonymous
class Tool { virtual void Placeholder() {} };class Hammer : public Tool { };class Thingy : public Hammer { };void f(Tool* obj){ cout << typeid(obj).name() << endl;}void main(void){ Tool* a; Hammer* b; Thingy* c; f(a); f(b); f(c);}
This obviously doesn''t work as one could wish it would. You probably guessed it already, it prints:
class Tool *
class Tool *
class Tool *
"Paranoia is the belief in a hidden order behind the visible." - Anonymous
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement