dynamic_cast如何在内部工作? [英] How dynamic_cast works internally?

查看:62
本文介绍了dynamic_cast如何在内部工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我能理解static_cast,reinterpret_cast和const_cast,他们

都在编译时工作。但我可以弄清楚C ++的动态 -

演员如何运作?你能帮我解释一下吗?

提前致谢!


问候!

Bo

解决方案

10月22日下午1:51,Bo Yang<奋斗... @ gmail.comwrote:




我能理解static_cast,reinterpret_cast和const_cast,他们

都在编译时工作。但我可以弄清楚C ++的动态 -

演员如何运作?你能帮我解释一下吗?

提前致谢!


问候!

Bo


动态演员将相应类型的偏移地址对应地说明。

如果它没有找到它将引发异常的类型(在
$中) b $ b参考案例)或者它将返回null。

Ex:

class base {

};


class base1 {

};

派生类:public base1,public base2 {

};

派生temp = new derived;

base2 * p = dynamic_cast< base2 * temp;


它会将prt偏移量输入base2部分临时对象。


Br,

akshay saidulu


10月22日,凌晨4:51,Bo Yang<奋斗... @ gmail.comwrote:




我能理解static_cast, reinterpret_cast和const_cast,他们

都在编译时工作。但我可以弄清楚C ++的动态 -

演员如何运作?你能帮我解释一下吗?

提前致谢!


问候!

Bo


考虑这种情况:

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

class B:public A {};

A * a =新B;

B * b = dynamic_cast< B *>(a);

从概念上讲,它就像这个if是用C写的:


void A_dtor(A *){}

typeinfo A_RTTI(){

static typeinfo

返回typeinfo(" A");

} //生成编译器

void(*)()A_vtbl [] = { A_dtor,A_RTTI};

struct A {

A_vtbl * vptr;

};

A_ctor(){ vptr = A_vtbl;} //生成编译器


void B_dtor(B *){}

typeinfo B_RTTI(){

static typeinfo

返回typeinfo(" B");

} //生成编译器

void(*)(B *)B_vtbl [ ] = {B_dtor,B_RTTI};

struct B {

B_vtbl * vptr;

};

B_ctor (){的vptr = B_vtbl;} // C生成的ompiler


这就是A和B在概念上的样子。现在

当调用dynamic_cast时,编译器会生成一个函数

类似


B * Dynamic_Cast(A * a){

if(*((a-> vptr)+1)()== B_RTTI())

返回a;

返回0;

}


>从这里可以很容易地推断如何将其扩展到工作



for references。


你现在可以看到为什么dynamic_cast只适用于

有虚拟的类型的原因;在某个地方,无论是功能还是继承。


Lance


10月22日凌晨2:20,Lance Diduck< lancedid ... @ nyc.rr.comwrote:


10月22日凌晨4:51,Bo Yang<奋斗...... @gmail。编写:


我能理解static_cast,reinterpret_cast和const_cast,他们

都在编译时工作。但我可以弄清楚C ++的动态 -

演员如何运作?你能帮我解释一下吗?

提前致谢!


问候!

Bo



考虑这个案例:

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

class B:public A {};

A * a =新B;

B * b = dynamic_cast< B *>(a);. 。


B * Dynamic_Cast(A * a){

if(*((a-> vptr)+1 )()== B_RTTI())

返回a;

返回0;


}



这个实现可能适用于typeid - 因为typeid必须

只确认被测对象的那种类型 - 匹配指定的

确切地说。


然而,dynamic_cast运算符必须执行更复杂的

测试:对象(未知类型)是否与另一种类型相关。

并且可以减少关于所涉及的类的信息,比上面的例子中存在的b $ b更多。例如:


struct A

{

virtual~A(){}

} ;


bool TestA(void * p)

{

断言(p!= NULL);

返回dynamic_cast< A *>(p)!= NULL;

}


请注意,没有A的派生类(如果存在)在编译这个

源文件时可见。因此,无论这个

程序的类层次结构可能有多大还是复杂,dynamic_cast<只有A

类声明,可以在这里工作。


那么dynamic_cast<应该如何判断p是否指向与A有某种关系的

类型?好吧,考虑到这些限制,

只有一种方法可以解决这个问题。 dynamic_cast必须搜索

p'的类层次结构(基本上,发现它的布局,因为它沿着它继续)并继续搜索直到找到A类

与层次结构 - 或者

层次结构中没有其他类需要检查。


Greg


Hi,
I can understand static_cast, reinterpret_cast and const_cast, they
all work at compile time. But I can figure out how the C++''s dynamic-
cast works? Could you please explain how for me?
Thanks in advance!

Regards!
Bo

解决方案

On Oct 22, 1:51 pm, Bo Yang <struggl...@gmail.comwrote:

Hi,
I can understand static_cast, reinterpret_cast and const_cast, they
all work at compile time. But I can figure out how the C++''s dynamic-
cast works? Could you please explain how for me?
Thanks in advance!

Regards!
Bo

Dynamic cast adjests the offset adress of the perticul type
accordigly .if it does''t find the type it will raise the exception (in
case of reference)or it will return null.
Ex:
class base{
};

class base1{
};
class derived : public base1,public base2{
};
deribed temp = new derived;
base2 *p = dynamic_cast<base2*temp;

it will adject the prt offset to base2 part of the temp object.

Br,
akshay saidulu


On Oct 22, 4:51 am, Bo Yang <struggl...@gmail.comwrote:

Hi,
I can understand static_cast, reinterpret_cast and const_cast, they
all work at compile time. But I can figure out how the C++''s dynamic-
cast works? Could you please explain how for me?
Thanks in advance!

Regards!
Bo

Consider this case:
class A{virtual ~A(){};};
class B:public A{};
A*a=new B;
B*b =dynamic_cast<B*>(a);
Conceptually, it is like this if is were written in C:

void A_dtor(A* ){}
typeinfo A_RTTI(){
static typeinfo
return typeinfo("A");
}//compiler generated
void(*)() A_vtbl[]={A_dtor,A_RTTI};
struct A{
A_vtbl*vptr;
};
A_ctor(){vptr=A_vtbl;}//compiler generated

void B_dtor(B* ){}
typeinfo B_RTTI(){
static typeinfo
return typeinfo("B");
}//compiler generated
void(*)(B*) B_vtbl[]={B_dtor,B_RTTI};
struct B{
B_vtbl*vptr;
};
B_ctor(){vptr=B_vtbl;}//compiler generated

This is what A and B conceptually look like underneath the hood. Now
when dynamic_cast is called, the compiler generates a function
something like

B* Dynamic_Cast(A*a ){
if(*((a->vptr)+1)()==B_RTTI())
return a;
return 0;
}

>From this, it is easy to extrapolate how this can be extended to work

for references.

You can see the reason now why dynamic_cast only works for types that
have "virtual" somewhere, either a function or inheritance.

Lance


On Oct 22, 2:20 am, Lance Diduck <lancedid...@nyc.rr.comwrote:

On Oct 22, 4:51 am, Bo Yang <struggl...@gmail.comwrote:Hi,

I can understand static_cast, reinterpret_cast and const_cast, they
all work at compile time. But I can figure out how the C++''s dynamic-
cast works? Could you please explain how for me?
Thanks in advance!

Regards!
Bo


Consider this case:
class A{virtual ~A(){};};
class B:public A{};
A*a=new B;
B*b =dynamic_cast<B*>(a);. .

B* Dynamic_Cast(A*a ){
if(*((a->vptr)+1)()==B_RTTI())
return a;
return 0;

}

This implementation might work for typeid - because typeid has to
confirm only that type of the object being tested - matches the
specified type exactly.

A dynamic_cast operator however has to perform a much more complicated
test: whether an object (of unknown thype) is related to another type.
And to make do with a lot less information about the classes involved
than existed in the example above. For example:

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

bool TestA(void *p)
{
assert( p != NULL);
return dynamic_cast<A*>(p) != NULL;
}

Note that no derived classes of A (if any exist) are visible when this
source file is compiled. So no matter how many large or complex this
program''s class hierarchy might be, dynamic_cast<has only the "A"
class declaration with which to work here.

So how is dynamic_cast<supposed to figure out whether p points to a
type that is somehow related to A? Well, given these constraints,
there is only one way to solve this problem. dynamic_cast must search
p''s class hierarchy (essentially, discovering its layout as it goes
along) and to continue the search until either a A class is located
with the hierarchy - or there are no other classes left in the
hierarchy that need to be checked.

Greg


这篇关于dynamic_cast如何在内部工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆