473,785 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem casting from a vector

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 store it in a
DerivedClass variable, doing:

DerivedClass d =
(DerivedClass)m yVector[(int)dataTables Vector.size()-1];

this gives me the following error:

error C2440: 'type cast' : cannot convert from
'std::allocator <_Ty>::value_ty pe' to 'DerivedClass'
with
[
_Ty=BaseClass *
]
Where am I going wrong?

Thanks.

Jul 23 '05 #1
3 2566
DJ*********@gma il.com wrote:
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 store it in a
DerivedClass variable, doing:
That doesn't make much sense. If it is a BaseClass variable, how would you
copy its contents into a DerivedClass one? What would the myVector of the
copy be initialized to?
DerivedClass d =
(DerivedClass)m yVector[(int)dataTables Vector.size()-1];
Don't use C style casts. They are a big no-no in C++.
this gives me the following error:

error C2440: 'type cast' : cannot convert from
'std::allocator <_Ty>::value_ty pe' to 'DerivedClass'
with
[
_Ty=BaseClass *
]
Where am I going wrong?


You are trying to copy a BaseClass object into a DerivedClass one. That's
only possible if you declare a conversion constructor or conversion
operator that does this. But I don't see how that could work. Maybe you
actually want a reference?
Jul 23 '05 #2

<DJ*********@gm ail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
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 store it in a
DerivedClass variable, doing:

DerivedClass d =
(DerivedClass)m yVector[(int)dataTables Vector.size()-1];

this gives me the following error:

error C2440: 'type cast' : cannot convert from
'std::allocator <_Ty>::value_ty pe' to 'DerivedClass'
with
[
_Ty=BaseClass *
]
Where am I going wrong?


Forget the code. The logic doesn't make sense. Why are you storing base
class objects in a Derived class? Can you store a vector of vehicles in a
car? Would you store a vector of shapes in a triangle? Why would you store
Animals in a Chicken?

Inheritence only works when a strict relationship exists between the base
and its derivative. Its an exceptional relationship.

Storing a type in a class is called composition. A Car has a motor. A truck
has a vector of wheels. An airplane has a landing gear. A building has a
vector of floors. A Ferry can hold vehicles, etc...

Also, as already mentioned to you, casting is not guesswork in C++. This is
a language with strong type checking which extends to its casting mechanisms
(static_cast, dynamic_cast, etc). Casting from a base class to a derived
class implies that the object is a derived object to begin with.

Jul 23 '05 #3
Peter Julian wrote:
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 store it in a
DerivedClass variable, doing:

DerivedClass d =
(DerivedClass)m yVector[(int)dataTables Vector.size()-1];

this gives me the following error:

error C2440: 'type cast' : cannot convert from
'std::allocator <_Ty>::value_ty pe' to 'DerivedClass'
with
[
_Ty=BaseClass *
]
Where am I going wrong?

Forget the code. The logic doesn't make sense. Why are you storing base
class objects in a Derived class? Can you store a vector of vehicles in a
car?


Well, there are trucks that are used to transport cars.
Would you store a vector of shapes in a triangle?


Not in a triangle, but what about a compound object that is composed of
several simple shapes? Most 2D drawing programs offer something like that.

This structure is also called the composite design pattern, and it's not so
uncommon.

Jul 23 '05 #4

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

Similar topics

5
1544
by: Jason Heyes | last post by:
I didn't want to repeat the same code so I casted to const. Here is a simplified example of when casting to const can help avoid code duplication: class MyClass { int numbers; public: // default constructor not shown
21
2192
by: Makhno | last post by:
Hello, Why does my cast from Vector<class Float> to Vector<float> not work? It won't compile, template<class Float> class Vector { public: Vector(Float x1,Float y1,Float z1):x(x1),y(y1),z(z1){} inline Vector<float> operator() () const;
14
1422
by: laniik | last post by:
Hi. Im just wondering what the syntax to overload a casting operator is. For example, i have struct Point { float x,y,z; }; struct Vector { float a,b,c; };
1
340
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 store it in a DerivedClass variable, doing: DerivedClass d = (DerivedClass)myVector;
17
16615
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee part of Manager is copied. ".... and gives the code above as .....
39
19647
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
1
1001
by: Herby | last post by:
Can generic types be casted? say Vector<Type^>^ to Vector<CodeType^>^ Where CodeType is a derivative of Type Is this possible ? Or is there something quite static about generics?
4
5331
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; std::vector<Derived*>::iterator d;
10
3602
by: Dom Jackson | last post by:
I have a program which crashes when: 1 - I use static_cast to turn a base type pointer into a pointer to a derived type 2 - I use this new pointer to call a function in an object of the derived type 3 - this function then 'grows' the derived type object (by pushing onto a vector).
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10152
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.