从 baseclassptrVec 中铸造衍生类ptrVector,最好的方法是什么?

发布于 2024-10-09 05:57:00 字数 171 浏览 0 评论 0原文

我正在获取需要传递给派生类指针向量的基类指针向量,请建议执行相同操作的最佳方法。

一种方法是

使用基类 ptr 向量大小创建一个新的派生类向量

,循环传入向量

动态转换基指针并填充派生向量。

请建议是否有更好的方法来说服编译器该向量适用于多态类型。

I am getting base class pointer vector that needed to be passed to derived class pointer vector, please suggest best way to do the same.

One way will be

create a new derived class vector with the base class ptr vector size

loop the incoming vector

dynamic cast the base pointer and fill the derived vector.

Please suggest if there is some better way to do convince the compile that the vector is for the polymorphic types.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朱染 2024-10-16 05:57:00

如果我对问题的理解正确,那么您会遇到以下情况

class Base {};
class Derived: public Base {};
class Derived2: public Base {};

void foo(const vector<Derived*>&);

void bar()
{
  vector<Base*> baseVec;

  // Fill baseVec with pointers to Derived objects

  foo(baseVec); // Does not work
}

问题是,尽管 BaseDerived 通过继承相关,但类型 vector;vector 完全不相关。两者之间没有转换。
此外,还有一个问题是 baseVec 还可能包含指向其他派生类的指针,例如 Derived2foo 无法处理这些。

唯一真正的解决方案是

  • 问题中概述的解决方案:创建一个新的向量Derived*>并使用源向量的dynamic_casted内容填充它,
  • 避免出现这样的情况程序中的类型不匹配。

If I understand the question right, you have the following situation

class Base {};
class Derived: public Base {};
class Derived2: public Base {};

void foo(const vector<Derived*>&);

void bar()
{
  vector<Base*> baseVec;

  // Fill baseVec with pointers to Derived objects

  foo(baseVec); // Does not work
}

The problem is that, although Base and Derived are related through inheritance, the types vector<Base*> and vector<Derived*> are completely unrelated. There is no conversion between the two.
Also, there is the additional problem that baseVec could also contain pointers to other derived classes, such as Derived2, and foo can't handle these.

The only real solutions are

  • the one outlined in the question: Create a new vector<Derived*> and populate that with dynamic_casted content of the source vector
  • avoid having such a type mismatch in the program.
零度℉ 2024-10-16 05:57:00

我觉得你说的这种方式是最合法的。
如果您必须强制转换向量本身,那么像下面这样的断言函数是否可以满足您的目的?

struct A {
  virtual ~A() {}
};

struct B : A {};

template< class Derived, class Base >
vector< Derived > const* dynamic_cast_vector( vector< Base > const& x )
{
  for ( typename vector< Base >::const_iterator i = x.begin(), e = x.end();
        i != e;
        ++ i ) {
    if ( ! dynamic_cast< Derived >( *i ) ) return 0;
  }

  return (vector< Derived > const*) &x;
}

int main()
{
  vector< A* >  av( 1, new A ), bv( 1, new B );
  dynamic_cast_vector< B* >( av ); // null
  dynamic_cast_vector< B* >( bv ); // ok
}

希望这有帮助

I think what you mentioned is the most legal way.
If you have to cast the vector itself, will some assertion function like the following serve your purpose?

struct A {
  virtual ~A() {}
};

struct B : A {};

template< class Derived, class Base >
vector< Derived > const* dynamic_cast_vector( vector< Base > const& x )
{
  for ( typename vector< Base >::const_iterator i = x.begin(), e = x.end();
        i != e;
        ++ i ) {
    if ( ! dynamic_cast< Derived >( *i ) ) return 0;
  }

  return (vector< Derived > const*) &x;
}

int main()
{
  vector< A* >  av( 1, new A ), bv( 1, new B );
  dynamic_cast_vector< B* >( av ); // null
  dynamic_cast_vector< B* >( bv ); // ok
}

Hope this helps

梦与时光遇 2024-10-16 05:57:00

如果您确定您的 baseclassptrVector 仅具有 衍生类 指针,则可以使用以下方法绕过 C++ 类型检查:

derivedclassptrVector = reinterpret_cast<std::vector<derivedclass *> &>(baseclassptrVector);

If you are sure that your baseclassptrVector has only derivedclass pointers, you can circumvent the C++ type checking with:

derivedclassptrVector = reinterpret_cast<std::vector<derivedclass *> &>(baseclassptrVector);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文