简体   繁体   English

如果派生类包含其他方法和成员,那么static_cast可以从基类到派生类完成吗?

[英]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 ”, where B is a class type, can be converted to a prvalue of type “pointer to cv2 D ”, where D is a class derived (Clause 10) from B , if cv2 is the same cv-qualification as, or greater cv-qualification than, cv1 . 类型为“指向cv1 B指针”的prvalue,其中B是类类型,可以转换为类型为“指向cv2 D指针”的prvalue,其中D是从B派生的类(第10条),如果cv2是与cv1相同的cv资格,或更高的cv资格。 If B is a virtual base class of D or a base class of a virtual base class of D , or if no valid standard conversion from “pointer to D ” to “pointer to B ” 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 a B that is actually a subobject of an object of type D , the resulting pointer points to the enclosing object of type D . 如果类型的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.

相关问题 什么时候是`static_cast <Base*> (的static_cast <void*> (派生))`从指向派生类的指针有效吗? - When is a `static_cast<Base*>(static_cast<void*>(derived))` from a pointer to a derived class valid? 将派生对象中的“this”指针的static_cast用于基类的问题 - Question of using static_cast on “this” pointer in a derived object to base class "static_cast 将此对象派生到 C++ 中的基类" - static_cast derived this object to base class in C++ 您能否在基本 class 构造函数中将“this”静态转换为派生的 class,然后再使用结果? - Can you static_cast "this" to a derived class in a base class constructor then use the result later? 从基类指针到派生类指针的static_cast无效 - static_cast from base class pointer to derived class pointer is invalid 当static_cast从派生类到基类时会发生什么? - What happens when static_cast from derived class to base class? C2440 static_cast无法从基类转换为派生类 - C2440 static_cast cannot convert from base class to derived class static_cast派生类的接口 - static_cast an interface to derived class 当调用派生类对象时,为什么`this`无法从基类方法访问派生类成员 - Why `this` can't access the derived class members from base class methods when called for derived class object static_cast到base析构函数的指向派生类的安全性 - Safety of static_cast to pointer-to-derived class from base destructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM