Re: typeid does not return polymorphic type
gast128@hotmail.com ha scritto:
in "example.cpp":
//#include "base.hpp"
//#include "derived.hpp"
void Foo(Base* p)
{
std::cout << typeid(*p).name() << std::endl; //ALWAYS class Base
}
This doesn't compile at all. You don't have neither a definition nor a
declaration of Base in that code. However, a declaration is not
sufficient to use typeid, you *must* fully define Base by including
"base.hpp" (see 5.2.8/3) and the compiler should reject the code if you
don't do that.
On the other hand, the inclusion of "derived.hpp" is totally
inessential. The definition of Base is enough to determine that it is a
polymorphic type.
Uncommenting the "derived.hpp" in example.cpp will give the correct
polymorphic class. Uncommenting the "base.hpp " will give in my simple
test application also the correct result, but in my real application
even that doesn't work. I use Visual Studio 2003 (with /GR compiled),
but is this a m$ bug or is this behavior compiler dependent?
I'm sorry, but I find it hard to believe that. The behaviour is not
compiler dependent and so you would be experiencing a bug, but I'm
pretty sure VS does not (mis-)behave like that. How are you detecting
that the program does not behave as it should? Could you post a
*complete* and compiling code snippet that shows that?
HTH,
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]