简体   繁体   English

static_cast <>中的C ++编译错误

[英]C++ compilation error in static_cast< >

Below is my code 下面是我的代码

Class A
{
  A::A(int num) { }
  int num;
};

class B : public A
{
  B::B(int num):A(num) { }
};

Class D;
Class C
{
  void getNum(A**& somenum) {}
  D *dObj;
};

void C::getNum(A**& somenum)
{
  dObj->getNumber(static_cast<B**>(somenum)); // Error here.
}

Class D
{
  void getNumber(B**& number) 
  { 
      B someValue[5];
      // all the objects in the array are properly created and properly Initalized (skipped that part)

      number[0] = someValue[0]; 
      number[1] = someValue[1];
      //...
   }
};

I'm getting compilation error while doing the static_cast. 执行static_cast时出现编译错误。 I am trying to assign the values in "someValue" array to "A**& somenum". 我正在尝试将“ someValue”数组中的值分配给“ A **&somenum”。 Can you please help how to do it. 你能帮忙怎么做吗?

Thank you very much in advance. 提前非常感谢您。

void C::getNum(A**& somenum)
{
  dObj->getNumber(static_cast<B**>(somenum)); // Error here.
}

static_cast is used to perform conversions between pointers to related classes, or to perform any other non-pointer conversion that could also be performed implicitly. static_cast用于在指向相关类的指针之间执行转换,或执行也可以隐式执行的任何其他非指针转换。 which is not the case in your above example. 在上面的示例中情况并非如此。 so that's why static_cast can't be used here. 所以这就是为什么static_cast不能在这里使用的原因。

Because C++ supports multiple inheritance (and other features), an A * is convertible to a B * but not necessarily identical to a B * . 因为C ++支持多重继承(和其他功能),所以A *转换B *但不一定 B * 相同 Converting between the pointer types could move the pointer value by a some number of bytes. 在指针类型之间进行转换可能会使指针值移动一定数量的字节。

As a result, it's impossible to convert an A ** to a B ** or vice versa. 结果,不可能将A **转换为B ** ,反之亦然。 The base-to-derived conversion would need to happen after retrieving the value from the outer pointer. 从外部指针检索值之后,将需要进行从基数到源的转换。

Multiple indirection is usually a bad idea. 多次间接访问通常不是一个好主意。 It's hard to know what data structure might help, but a review of the standard library containers might be insightful. 很难知道哪种数据结构可能会有所帮助,但是对标准库容器的回顾可能会很有见地。 Anyway, thanks for condensing down this nice self-contained example! 无论如何,感谢您总结这个很好的独立示例!

Your compiler is doing the right thing here. 您的编译器在这里做正确的事。 In short, you can't do that . 简而言之, 您不能那样做 You can convert between a base class pointer and a derived class pointer, provided they point to the same object. 您可以在基类指针和派生类指针之间进行转换,前提是它们指向同一对象。 But an array-of-Base and array-of Derived are not the same thing . 但是,Base-of-Base和Derived数组不是一回事

The good news is what you want to do is easier than you think. 好消息是您想要做的比您想的要容易。 A derived class pointer already implicitly converts to a Base class pointer without casting. 派生类指针已隐式转换为基类指针,而无需强制转换。 Assuming you know the sizes of your two arrays, and the sizes are the same, it's a simple loop: 假设您知道两个数组的大小,并且大小相同,这是一个简单的循环:

// Given:
A** someNumbers;
B** someValues;

for (int i = 0; i < size; ++i) {
    *someNumbers[i] = *someValues[i];
}

Also, this sort of problem is why we have standard containers like vector . 同样,这种问题就是为什么我们有像vector这样的标准容器。 Some really smart people have already solved this pointer madness for you. 一些真正聪明的人已经为您解决了这种指针疯狂问题。 I highly recommend taking advantage of it. 我强烈建议您利用它。 It'll make your C++ experience a ton better. 它将使您的C ++体验更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM