473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

vector casting

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?
Nov 25 '06 #1
6 2592
Chameleon wrote:
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.

--
Ian Collins.
Nov 25 '06 #2
Chameleon wrote:
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<A*and vector<B*are different.
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.
Nov 25 '06 #3
Chameleon wrote:
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*>.
Nov 25 '06 #4
On Sat, 25 Nov 2006 04:12:30 +0200, Chameleon wrote:
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);
Nov 25 '06 #5
"Chameleon" <ch******@hotmail.comwrote in message
news:ek**********@volcano1.grnet.gr...
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 );
}
Nov 25 '06 #6

Chameleon wrote:
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.

Nov 25 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: enzo | last post by:
hi all, i don't understand what's wrong: 1) std::vector<double> p(10); doesn't compile:
2
by: Pepijn Kenter | last post by:
Dear experts. I have a vector<float> and want to convert that to a vector<double>. I optimistically tried: #include <vector> #include <iostream> using namespace std; int main() {
4
by: misirion | last post by:
Ciao, vorrei poter scrivere e leggere su files binari dei vettori di stringhe, ed avrei implementato questo codice: ifstream fin(conf.c_str(),ios::binary); char inc; fin.read( inc,...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
3
by: DJ.precario | last post by:
Hi, I have a vector (myVector) of BaseClass objects. I store in it a DerivedClass object (where DerivedClass inherits from BaseClass). Later I want to get the last element in the vector and...
33
by: Simon | last post by:
Hi, I am going to use quite a few vectors and I want to make sure I am using it properly. // struct MYSTRUCT { .... }
8
by: Joseph Turian | last post by:
Some function requires a vector<const foo*> argument. How can I cast a vector<foo*> to vector<const foo*>? Thanks! Joseph
4
by: propokergrad | last post by:
Hello, say I have two classes: class Base{...}; class Derived : public Base{...} I would like to do something similar to this: std::vector<Base*>::iterator b;...
1
by: efittery | last post by:
I need to modify the following code to handle a vector of pairs of floats. I was just thinking of casting my vector of floats into being a vector of pairs of floats. Alternately, I have...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.