[英]Can static_cast be done from base class to derived class if derived class contains additional methods and members?
Suppose we have class A and B as follows: 假设我们有A类和B类如下:
class A
{
private:
int a;
public:
void seta(int a_)
{
a=a_;
}
int geta()
{
return a;
}
};
class B: public A
{
private:
int b;
public:
int getb()
{
return b;
}
void setb()
{
b=geta()+1;
}
};
and suppose that I make such code in the function: 并假设我在函数中创建了这样的代码:
A* a=new A();
a->seta(5);
B* b=static_cast<B*>(a);
b->setb();
cout<<b->getb()<<" and "<<b->geta()<<endl;
This code compiles and runs, but it confuses me why? 这段代码编译并运行,但它让我困惑为什么? If
a
is pointer to A
class and during allocation only memory for class A
members is reserved (at runtime), why after static cast it seems that this object is actually instance of class B
. 如果
a
是指向A
类的指针,并且在分配期间只保留了class A
成员的内存(在运行时),为什么在静态转换之后看起来这个对象实际上是class B
实例。 Is this safe operation? 这是安全的操作吗?
[expr.static.cast]/11 , emphasis mine: [expr.static.cast] / 11 ,强调我的:
A prvalue of type “pointer to cv1
B
”, whereB
is a class type, can be converted to a prvalue of type “pointer to cv2D
”, whereD
is a class derived (Clause 10) fromB
, if cv2 is the same cv-qualification as, or greater cv-qualification than, cv1 .类型为“指向cv1
B
指针”的prvalue,其中B
是类类型,可以转换为类型为“指向cv2D
指针”的prvalue,其中D
是从B
派生的类(第10条),如果cv2是与cv1相同的cv资格,或更高的cv资格。 IfB
is a virtual base class ofD
or a base class of a virtual base class ofD
, or if no valid standard conversion from “pointer toD
” to “pointer toB
” exists (4.11), the program is ill-formed.如果
B
是虚拟基类的D
或基类的虚拟基类中的D
,或如果从“指针没有有效的标准转换D
”到“指针B
”的存在(4.11),是形成不良的程序。 The null pointer value (4.11) is converted to the null pointer value of the destination type.空指针值(4.11)将转换为目标类型的空指针值。 If the prvalue of type “pointer to cv1
B
” points to aB
that is actually a subobject of an object of typeD
, the resulting pointer points to the enclosing object of typeD
.如果类型的prvalue“指针CV1
B
”指向一个B
实际上是类型的对象的子对象D
,将所得指针指向类型的包围对象D
。 Otherwise, the behavior is undefined.否则,行为未定义。
This is not a safe thing to do. 这不是一件安全的事情。 The following code demonstrates why
以下代码说明了原因
#include <iostream>
using namespace std;
class A
{
private:
int a;
public:
void seta(int a_) {
a=a_;
cout << "seta to: " << a << endl;
}
int geta() {
return a;
}
};
class B: public A
{
private:
int b;
public:
int getb() {
return b;
}
void setb() {
b=geta()+1;
cout << "setb to: " << b << endl;
}
};
int main() {
A as[2];
A* a1=as;
A* a2=&as[1];
a1->seta(5);
a2->seta(4);
cout << "a1: "
<< a1->geta()
<< endl;
cout << "a2: "
<< a2->geta()
<< endl;
B* b=static_cast<B*>(a1);
b->setb();
a2->seta(3);
cout << "b->geta(): "
<< b->geta()
<<" and b->getb(): "
<< b->getb()
<< endl;
size_t sizeofa(sizeof(A));
cout << "sizeofa: "
<< sizeofa
<< endl;
size_t sizeofb(sizeof(B));
cout << "sizeofb: "
<< sizeofb
<< endl;
}
The output is 输出是
seta to: 5
seta to: 4
a1: 5
a2: 4
setb to: 6
seta to: 3
b->geta(): 5 and b->getb(): 3
sizeofa: 4
sizeofb: 8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.