在多级继承 C++ 之间进行转换和释放
Casting and freeing between multiple levels of inheritance c++
转换时顺序是否重要?这对释放具有虚拟基础的对象有影响吗?class?例如。如果你向上转换 2 级,你必须向下转换 2 级,还是可以一次向下转换 1 级?
struct A {
virtual ~A() = default;
...
};
struct A_Extra : A {...};
struct B : A_Extra {...};
std::vector<A*> data;
void scenario1() {
// create sample object - incremental upcasting
A_Extra* b = new B();
data.push_back(b);
A* a = data.front();
// which one(s) of these casts are valid ?
B* option1 = static_cast<B*>(static_cast<A_Extra*>(a));
B* option2 = static_cast<B*>(a);
// which one(s) of these frees are valid ?
delete a;
delete static_cast<A_Extra*>(a);
delete static_cast<B*>(static_cast<A_Extra*>(a));
delete static_cast<B*>(a);
}
void scenario2() {
// create sample object
B* b = new B();
data.push_back(b);
A* a = data.front();
// which one(s) of these casts are valid ?
B* option1 = static_cast<B*>(static_cast<A_Extra*>(a));
B* option2 = static_cast<B*>(a);
// which one(s) of these frees are valid ?
delete a;
delete static_cast<A_Extra*>(a);
delete static_cast<B*>(static_cast<A_Extra*>(a));
delete static_cast<B*>(a);
}
Does order matter when casting
是的,铸造时顺序很重要。
does this have an effect on freeing ...
None 的施法有效。
... with a virtual base class?
None 个 class 有一个虚拟基地 class。
E.g. if you upcasted 2 levels, must you downcast 2 levels, or can you downcast 1 level at a time?
您可以一次向上转换和向下转换多个级别,向下转换的次数少于向上转换的次数。
// which one(s) of these casts are valid ?
两者都有效。
// which one(s) of these frees are valid ?
全部有效(只要使用其中一个)
答案适用于两种情况。
转换为 B*
和 A_Extra*
有潜在的危险,因为 A*
可能不会指向此类对象。在这些情况下,他们会这样做,如果你能保证这一点,那么演员表就是有效的。在无法保证的情况下,dynamic_cast
是更安全的选择。
无论如何,这两个场景中的所有演员表都是多余的。最好不要使用不必要的转换。
转换时顺序是否重要?这对释放具有虚拟基础的对象有影响吗?class?例如。如果你向上转换 2 级,你必须向下转换 2 级,还是可以一次向下转换 1 级?
struct A {
virtual ~A() = default;
...
};
struct A_Extra : A {...};
struct B : A_Extra {...};
std::vector<A*> data;
void scenario1() {
// create sample object - incremental upcasting
A_Extra* b = new B();
data.push_back(b);
A* a = data.front();
// which one(s) of these casts are valid ?
B* option1 = static_cast<B*>(static_cast<A_Extra*>(a));
B* option2 = static_cast<B*>(a);
// which one(s) of these frees are valid ?
delete a;
delete static_cast<A_Extra*>(a);
delete static_cast<B*>(static_cast<A_Extra*>(a));
delete static_cast<B*>(a);
}
void scenario2() {
// create sample object
B* b = new B();
data.push_back(b);
A* a = data.front();
// which one(s) of these casts are valid ?
B* option1 = static_cast<B*>(static_cast<A_Extra*>(a));
B* option2 = static_cast<B*>(a);
// which one(s) of these frees are valid ?
delete a;
delete static_cast<A_Extra*>(a);
delete static_cast<B*>(static_cast<A_Extra*>(a));
delete static_cast<B*>(a);
}
Does order matter when casting
是的,铸造时顺序很重要。
does this have an effect on freeing ...
None 的施法有效。
... with a virtual base class?
None 个 class 有一个虚拟基地 class。
E.g. if you upcasted 2 levels, must you downcast 2 levels, or can you downcast 1 level at a time?
您可以一次向上转换和向下转换多个级别,向下转换的次数少于向上转换的次数。
// which one(s) of these casts are valid ?
两者都有效。
// which one(s) of these frees are valid ?
全部有效(只要使用其中一个)
答案适用于两种情况。
转换为 B*
和 A_Extra*
有潜在的危险,因为 A*
可能不会指向此类对象。在这些情况下,他们会这样做,如果你能保证这一点,那么演员表就是有效的。在无法保证的情况下,dynamic_cast
是更安全的选择。
无论如何,这两个场景中的所有演员表都是多余的。最好不要使用不必要的转换。