C++ 继承向下转型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11855018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ inheritance downcasting
提问by CodeKingPlusPlus
I have my base class as follows:
我的基类如下:
class point //concrete class
{
... //implementation
}
class subpoint : public point //concrete class
{
... //implementation
}
How do I cast from a point object to a subpoint object? I have tried all three of the following:
如何从点对象投射到子点对象?我已经尝试了以下所有三个:
point a;
subpoint* b = dynamic_cast<subpoint*>(&a);
subpoint* b = (subpoint*)a;
subpoint b = (subpoint)a;
What is wrong with these casts?
这些演员有什么问题?
回答by Mike Seymour
How do I cast from a point object to a subpoint object?
如何从点对象投射到子点对象?
You can't; unless either point
has a conversion operator, or subpoint
has a conversion constructor, in which case the object types can be converted with no need for a cast.
你不能;除非point
有一个转换运算符,或者subpoint
有一个转换构造函数,在这种情况下,对象类型可以在不需要强制转换的情况下进行转换。
You could cast from a point
reference(or pointer) to a subpoint
reference(or pointer), if the referred object were actually of type subpoint
:
如果引用的对象实际上是类型,则可以从point
引用(或指针)转换为subpoint
引用(或指针)subpoint
:
subpoint s;
point & a = s;
subpoint & b1 = static_cast<subpoint&>(a);
subpoint & b2 = dynamic_cast<subpoint&>(a);
The first (static_cast
) is more dangerous; there is no check that the conversion is valid, so if a
doesn't refer to a subpoint
, then using b1
will have undefined behaviour.
第一个 ( static_cast
) 更危险;没有检查转换是否有效,因此如果a
不引用 a subpoint
,则 usingb1
将具有未定义的行为。
The second (dynamic_cast
) is safer, but will only work if point
is polymorphic (that is, if it has a virtual function). If a
refers to an object of incompatible type, then it will throw an exception.
第二个 ( dynamic_cast
) 更安全,但只有在point
多态时才有效(即,如果它有虚函数)。如果a
引用一个不兼容类型的对象,那么它将抛出异常。
回答by Carl
The purpose of a dynamic cast is to "check at run time if an object is of a certain type in the hierarchy". So now let's look at what you have:
动态转换的目的是“在运行时检查对象是否属于层次结构中的某种类型”。所以现在让我们看看你有什么:
- You have a point object. Not a subpoint.
- You're asking a dynamic cast if the object is a subpoint. It's not.
- Because its not a subpoint, dynamic_cast fails - its way of telling you that the object is not the type you're trying to cast it to.
- 你有一个点对象。不是子点。
- 如果对象是一个子点,您正在询问动态转换。它不是。
- 因为它不是一个子点,所以 dynamic_cast 失败了 - 它告诉您该对象不是您尝试将其转换为的类型。
By contrast, this would have worked:
相比之下,这会奏效:
subpoint c;
point *a = &c;
subpoint* b = dynamic_cast<subpoint*>(&a);
subpoint* b = (subpoint*)a;
回答by Mark Ransom
For the first example, dynamic_cast
only works if there's at least one virtual method in the base class. And if the object isn't actually of the type you're trying to cast, it will result in NULL.
对于第一个示例,dynamic_cast
仅当基类中至少有一个虚拟方法时才有效。如果该对象实际上不是您尝试强制转换的类型,它将导致 NULL。
For the second example you need &a
instead of a
, but once you've fixed that you'll get undefined behavior because the object type is wrong.
对于第二个示例,您需要&a
而不是a
,但是一旦您修复了该问题,您将获得未定义的行为,因为对象类型是错误的。
The third example requires an operator subpoint()
method in point
to do a conversion while creating a copy.
第三个示例需要一个operator subpoint()
方法point
来在创建副本时进行转换。
回答by MSN
Overall, this will not work because point
is not a subpoint
; only the reverse is true. However, there are other issues as well.
总的来说,这将不起作用,因为point
它不是subpoint
; 只有相反才是正确的。但是,还有其他问题。
In order:
为了:
subpoint* b = dynamic_cast<subpoint*>(&a);
dynamic_cast
only works on polymorphic types, i.e., types that declare at least one virtual function. My guess is that point
has no virtual functions, which means it cannot be used with dynamic_cast
.
dynamic_cast
仅适用于多态类型,即声明至少一个虚函数的类型。我的猜测是它point
没有虚函数,这意味着它不能与dynamic_cast
.
subpoint* b = (subpoint*)a;
For this cast to work, point
needs to declare a conversion operator to subpoint *
, e.g., point::operator subpoint *()
.
要使此转换起作用,point
需要将转换运算符声明为subpoint *
,例如,point::operator subpoint *()
。
subpoint b = (subpoint)a;
For this cast to work, point needs to declare a conversion operator to subpoint
orsubpoint
needs to have a constructor that takes a parameter convertable from point
.
为了使这个转换工作, point 需要声明一个转换运算符到subpoint
或subpoint
需要有一个构造函数,它接受一个可转换的参数point
。
回答by Luchian Grigore
What is wrong with these casts?
这些演员有什么问题?
The fact that you attempt to do them. A point
is not a subpoint
, I'd be surprised if it worked.
你试图做这些的事实。Apoint
不是 a subpoint
,如果它有效,我会感到惊讶。
回答by Daniel A. White
a
can't be made into a subpoint
. that implementation isn't there.
a
不能做成subpoint
. 那个实现不存在。