typeid和dynamic_cast,gcc 3.3 [英] typeid and dynamic_cast, gcc 3.3
问题描述
你好,
我对以下小程序的
感到非常惊讶:
- - 削减----
#include" iostream.h"
等级基础{
** public:
** base(){}
** virtual * ~base(){};
** void * printType(base *** obj);
};
派生类:公共基础{
** public:
** virtual * void * doIt()
** {
**** printType(this);
**} **
};
void base :: printType(base * obj){
** cout *<< *" type * is *" *<< * typeid(obj).name()*<< * endl;
** cout *< ;< * dynamic_cast< derived **>(obj)*<< * endl;
}
int main(int argc,char * argv []){
**派生* myObj;
** myObj.doIt();
}
< br $>
---- cut ----
打印:
类型是P4base
0xbfffec00
我的期望是看到派生的对于
类型名称。
即使它被识别为base
,RTTI也不应该能够动态转换
它到一个派生的对象报告
不是。
用gcc 3.3测试3.3.2。
有人能开导我吗?
问候,
安德烈亚斯
Hello,
I had been quite suprised by the
following little program:
---- cut ----
#include "iostream.h"
class base {
**public:
**base(){}
**virtual*~base(){};
**void*printType(base***obj);
};
class derived : public base {
**public:
**virtual*void*doIt()
**{
****printType(this);
**}**
};
void base::printType(base * obj) {
**cout*<<*"type*is*"*<<*typeid(obj).name()*<<*endl ;
**cout*<<*dynamic_cast<derived**>(obj)*<<*endl;
}
int main(int argc, char * argv[]) {
**derived*myObj;
**myObj.doIt();
}
---- cut ----
It prints:
type is P4base
0xbfffec00
My expectation was to see "derived" for
the type name.
And even if it is identified as "base"
the RTTI should not be able to dynamic-cast
it to a derived object which it is reported
not to be.
Tested with gcc 3.3 and 3.3.2.
Somebody able to enlighten me?
Greetings,
Andreas
推荐答案
Andreas Sch。写道:
Andreas Sch. wrote:
你好,
我对以下小程序感到非常惊讶:
---- cut - -
#include" iostream.h"
基类{
public:
base(){}
虚拟〜 base(){};
void printType(base * obj);
};
类派生:public base {
public:
virtual void doIt()
{
printType(this);
}
};
void base :: printType(base * obj){
cout<< 类型是 << typeid(obj).name()<< ENDL;
这不是虚函数,所以它将被调用类型为''this''
设置为base,即使它是从一个虚函数中调用的。
(virtual-ness不传递调用序列,它基于每个函数的b / b $ b $。)动态转换
cout<< dynamic_cast< derived *>(obj)<< ENDL;
我没有看到这样做的目的:它只是打印出指针
的值。你有没有忘记输入()它?
}
int main(int argc,char * argv []){
派生myObj;
myObj.doIt();
}
---- cut ----
Hello,
I had been quite suprised by the
following little program:
---- cut ----
#include "iostream.h"
class base {
public:
base(){}
virtual ~base(){};
void printType(base * obj);
};
class derived : public base {
public:
virtual void doIt()
{
printType(this);
}
};
void base::printType(base * obj) {
cout << "type is " << typeid(obj).name() << endl;
This isn''t a virtual function, so it will be called with type of ''this''
set to "base", even though it was called from a virtual function
(virtual-ness doesn''t pass along the calling sequence, its on a
per-function basis.) Dynamic-casting
cout << dynamic_cast<derived *>(obj) << endl;
I don''t see the purpose of doing this: it just prints out the pointer
value. Did you forget to typeid() it?
}
int main(int argc, char * argv[]) {
derived myObj;
myObj.doIt();
}
---- cut ----
--
http://www.it-is-truth.org/
Andreas Sch。写道:
Andreas Sch. wrote:
你好,
我对以下小程序感到非常惊讶:
---- cut - -
#include" iostream.h"
这个标题不是标准的 - 使用#include< iostream>
类库{
public:
base(){}
virtual~base (){};
^^^^^^^^'';''不需要
void printType(base * obj);
};
类派生:公共基地{
public:
virtual void doIt()
{
printType(this);
}
};
void base :: printType(base * obj){
cout<< 类型是 << typeid(obj).name()<< ENDL;
^^^^^^^^^^^^^^^ ...这与
cout< < 类型是 << typeid((base *)0).name()<< endl;
cout<< dynamic_cast< derived *>(obj)<< endl;
}
int main(int argc,char * argv []){
派生myObj;
myObj.doIt();
}
----剪切----
它打印:
类型是P4base
0xbfffec00
我的期望是看到派生的对于
类型名称。
即使它被识别为基础
,RTTI也不应该动态地将其转换为派生的报告的对象
不是。
使用gcc 3.3和3.3.2进行测试。
有人能够启发我吗?
Hello,
I had been quite suprised by the
following little program:
---- cut ----
#include "iostream.h" this header is not standard - use #include <iostream>
class base {
public:
base(){}
virtual ~base(){}; ^^^^^^^^ '';'' not needed
void printType(base * obj);
};
class derived : public base {
public:
virtual void doIt()
{
printType(this);
}
};
void base::printType(base * obj) {
cout << "type is " << typeid(obj).name() << endl;
^^^^^^^^^^^^^^^ ... this is the same as
cout << "type is " << typeid( (base *) 0 ).name() << endl;
cout << dynamic_cast<derived *>(obj) << endl;
}
int main(int argc, char * argv[]) {
derived myObj;
myObj.doIt();
}
---- cut ----
It prints:
type is P4base
0xbfffec00
My expectation was to see "derived" for
the type name.
And even if it is identified as "base"
the RTTI should not be able to dynamic-cast
it to a derived object which it is reported
not to be.
Tested with gcc 3.3 and 3.3.2.
Somebody able to enlighten me?
试试这个:
派生类;
#include< iostream>
使用命名空间std;
class base {
public:
base(){}
virtual~base(){}
模板< typename T>
void printType(T * obj)
{
cout<< 类型是 << typeid(obj).name()<< endl;
cout<< dynamic_cast< derived *>(obj)<<结束;
}
};
派生类:公共基础{
public:
虚拟无效doIt()
{
printType(this);
}
};
int main(int argc,char * argv []){
派生myObj;
myObj.doIt();
}
Try this:
class derived;
#include <iostream>
using namespace std;
class base {
public:
base(){}
virtual ~base(){}
template <typename T>
void printType(T * obj)
{
cout << "type is " << typeid(obj).name() << endl;
cout << dynamic_cast<derived *>(obj) << endl;
}
};
class derived : public base {
public:
virtual void doIt()
{
printType(this);
}
};
int main(int argc, char * argv[]) {
derived myObj;
myObj.doIt();
}
Asfand Yar Qazi写道:
Asfand Yar Qazi wrote:
(。 ..)
void base :: printType(base * obj){
cout<< 类型是 << typeid(obj).name()<< endl;
这不是虚函数,所以即使它是从虚函数中调用的,它也会被调用类型为''this''
设置为base。 >(virtual-ness不会传递调用序列,它基于每个函数。)动态转换
(...)
void base::printType(base * obj) {
cout << "type is " << typeid(obj).name() << endl;
This isn''t a virtual function, so it will be called with type of ''this''
set to "base", even though it was called from a virtual function
(virtual-ness doesn''t pass along the calling sequence, its on a
per-function basis.) Dynamic-casting
让我们看看是否我明白了:typeid没有评估内存中的
类型信息,但取决于obj类型的类型
。指针(而不是指向的指针)。
Lets see if I got it: typeid does not evaluate the
type information in memory but depends on the type
of the "obj" pointer (and not on what it is pointing to).
cout<< dynamic_cast< derived *>(obj)<< endl;
cout << dynamic_cast<derived *>(obj) << endl;
我没有看到这样做的目的:它只是打印出指针
值。你有没有忘记输入()?
I don''t see the purpose of doing this: it just prints out the pointer
value. Did you forget to typeid() it?
我只是想看看dynamic_cast是否失败以及
是否提供了0x0。我预计它会失败,因为打印出的bid字符串打印了基数字样。
感谢您的回答!
Andreas
I just wanted to see if the dynamic_cast fails and
delivers 0x0. I expected it to fail since the typeid-line
printed "base" and not "derived".
Thanks for your answer!
Andreas
这篇关于typeid和dynamic_cast,gcc 3.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!