avatar
dynamic_cast operator in C++# Programming - 葵花宝典
i*p
1
How to understand this sentence.
The class of the object passed to the dynamic_cast operator must be part of
the hierarchy the object is being cast to.
To discuss it easily, I have following statements.
A *ap;
....
B *bp = dynamic_cast(ap);
1. Which class, A or B is "The class of the object passed to the dynamic_
cast operator"? I guess it is A.
2. Class B should be "the object is being cast to", right?
avatar
d*d
2
right.

of

【在 i**p 的大作中提到】
: How to understand this sentence.
: The class of the object passed to the dynamic_cast operator must be part of
: the hierarchy the object is being cast to.
: To discuss it easily, I have following statements.
: A *ap;
: ....
: B *bp = dynamic_cast(ap);
: 1. Which class, A or B is "The class of the object passed to the dynamic_
: cast operator"? I guess it is A.
: 2. Class B should be "the object is being cast to", right?

avatar
d*d
3
dynamic_cast 一般是向下cast。
这句话的意思是指,B得是A的子类或孙子类....

of

【在 i**p 的大作中提到】
: How to understand this sentence.
: The class of the object passed to the dynamic_cast operator must be part of
: the hierarchy the object is being cast to.
: To discuss it easily, I have following statements.
: A *ap;
: ....
: B *bp = dynamic_cast(ap);
: 1. Which class, A or B is "The class of the object passed to the dynamic_
: cast operator"? I guess it is A.
: 2. Class B should be "the object is being cast to", right?

avatar
e*w
4
To make it a valid dynamic_cast, B should be descendant of A.
The object pointed by ap should be an object of B or B's descendant.

of

【在 i**p 的大作中提到】
: How to understand this sentence.
: The class of the object passed to the dynamic_cast operator must be part of
: the hierarchy the object is being cast to.
: To discuss it easily, I have following statements.
: A *ap;
: ....
: B *bp = dynamic_cast(ap);
: 1. Which class, A or B is "The class of the object passed to the dynamic_
: cast operator"? I guess it is A.
: 2. Class B should be "the object is being cast to", right?

avatar
i*p
5
So far no arguement that B得是A的子类或孙子类....
But can "the part" be interpreted as inherence?

【在 e*****w 的大作中提到】
: To make it a valid dynamic_cast, B should be descendant of A.
: The object pointed by ap should be an object of B or B's descendant.
:
: of

avatar
P*f
6
Besides downcast,
dynamic_cast is also used to support "cross cast" in MI.

To make it a valid dynamic_cast, B should be descendant of A.
The object pointed by ap should be an object of B or B's descendant.
of

【在 e*****w 的大作中提到】
: To make it a valid dynamic_cast, B should be descendant of A.
: The object pointed by ap should be an object of B or B's descendant.
:
: of

avatar
e*w
7
you're right. if the class hierarchy is A -> C as long as ap points to object of C (or its descendant),
then we can do this dynamic_cast, too.

【在 P*****f 的大作中提到】
: Besides downcast,
: dynamic_cast is also used to support "cross cast" in MI.
:
: To make it a valid dynamic_cast, B should be descendant of A.
: The object pointed by ap should be an object of B or B's descendant.
: of

avatar
i*p
8
Is "Class C, the class of the object that ap points to" the same as "The
class of the object passed to the dynamic_cast operator"?

【在 e*****w 的大作中提到】
: you're right. if the class hierarchy is A -> C : as long as ap points to object of C (or its descendant),
: then we can do this dynamic_cast, too.

avatar
e*w
9
yes.

【在 i**p 的大作中提到】
: Is "Class C, the class of the object that ap points to" the same as "The
: class of the object passed to the dynamic_cast operator"?

avatar
i*p
10
Make it more clear of my understanding.
A *ap = new C;
B *bp = dynamic_cast(ap);
"The class of the object passed to the dynamic_cast operator" is A, not C,
even though ap points to object of C.

【在 e*****w 的大作中提到】
: yes.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。