vector casting

C

Chameleon

class A {}
class B : public A {}

void proceed(vector<A*> &in);

void main() {
vector<B*> a;
proceed(a); // <-- error here! why?
}



Where is the problem?
I try any static_cast and others but nothing.
There is nothing to produce a problem because elements in any case are
pointers and basically we upcast all elements of vector a. Where is the
problem?
 
G

Gianni Mariani

Chameleon said:
class A {}
class B : public A {}

void proceed(vector<A*> &in);

void main() {
vector<B*> a;
proceed(a); // <-- error here! why?
}



Where is the problem?

vector said:
I try any static_cast and others but nothing.
There is nothing to produce a problem because elements in any case are
pointers and basically we upcast all elements of vector a. Where is the
problem?

If this was allowed by the compiler, you would inadvertently be allowed
to downcast.

Consider this:

class A {}
class B : public A { double d; }
class C : public A { float f; }

void proceed(vector<A*> &in)
{
in[0] = new C;
}

void main() {
vector<B*> a;
proceed(a);

a[0].d=3; // oops !
}

vector<A*> and vector<B*> are as different as class X {}; and class Y
{}; They're not related.
 
I

Ian Collins

Chameleon said:
class A {}
class B : public A {}

void proceed(vector<A*> &in);

void main() {
vector<B*> a;
proceed(a); // <-- error here! why?

vector<A*> and vector<B*> are different types.
 
M

Mark P

Chameleon said:
class A {}
class B : public A {}

void proceed(vector<A*> &in);

void main() {
vector<B*> a;
proceed(a); // <-- error here! why?
}



Where is the problem?
I try any static_cast and others but nothing.
There is nothing to produce a problem because elements in any case are
pointers and basically we upcast all elements of vector a. Where is the
problem?

Just because B derives from A does not imply that vector<B*> derives
from vector<A*>-- indeed it does not. Among other possibilities, you
can declare the vector as vector<A*>, copy the contents of vector<B*>
to a vector<A*>, or overload the function to accept a vector<B*>.
 
A

Amadeus W.M.

class A {}
class B : public A {}

void proceed(vector<A*> &in);

void main() {
vector<B*> a;
proceed(a); // <-- error here! why?
}



Where is the problem?
I try any static_cast and others but nothing.
There is nothing to produce a problem because elements in any case are
pointers and basically we upcast all elements of vector a. Where is the
problem?


Because you're declaring proceed(vector<A*> &) but you call it on
vector<B*>, I'm guessing you want proceed() to work on both vector<A*>
and vector<B*> (which are unrelated types), or on
vector<other_descendants_of_A>. If so, make proceed() a template:

template <class X>
void proceed(vector<X*> & a);
 
J

Jim Langston

Chameleon said:
class A {}
class B : public A {}

void proceed(vector<A*> &in);

void main() {
vector<B*> a;
proceed(a); // <-- error here! why?
}

Where is the problem?
I try any static_cast and others but nothing.
There is nothing to produce a problem because elements in any case are
pointers and basically we upcast all elements of vector a. Where is the
problem?

an A pointer can contain a B pointer with polymorphism, but a B pointer
can't contain an A pointer.

Maybe you are thinking of this.

class A{};
class B: public A {};

void proceed( std::vector<A*>& in );

void main()
{
std::vector<A*> a;
// even though this is a vector of A*, you can still push B's onto it.
a.push_back( new B() );

proceed( a );
}
 
S

Salt_Peter

Chameleon said:
class A {}
class B : public A {}

void proceed(vector<A*> &in);

void main() {
vector<B*> a;
proceed(a); // <-- error here! why?
}



Where is the problem?
I try any static_cast and others but nothing.
There is nothing to produce a problem because elements in any case are
pointers and basically we upcast all elements of vector a. Where is the
problem?

If you had a std::vector<A*> with B* elements, then you'ld proceed(a)
with no problems.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top