0

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.

Learner
  • 597
  • 1
  • 5
  • 17
  • Are you sure that all objects in the base class pointer are actually derived class objects ? Otherwise casting will fail. Just think about what happens if somebody writes a new derived class and passes a pointer of that. I think you should take a relook at your design. – Naveen Dec 29 '10 at 08:39
  • I read this backwards at first. As Naveen says, what you are doing is probably inadvisable and indicative that there could be a better design using virtual methods. The other way...casting a vector of derived class pointers to a vector of base class pointers...is reasonable to want, yet not legal in C++ as templates do not support "covariance": http://stackoverflow.com/questions/3922335/reference-to-a-stdvector-of-derived-class-pointers-as-input-parameter-to-a-func – HostileFork says dont trust SE Dec 29 '10 at 09:05

3 Answers3

2

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.
Bart van Ingen Schenau
  • 15,488
  • 4
  • 32
  • 41
1

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

Ise Wisteria
  • 11,259
  • 2
  • 43
  • 26
0

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);
0xF
  • 3,214
  • 1
  • 25
  • 29