473,770 Members | 6,978 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 2565
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
16610
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
1000
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
5330
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
3600
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
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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
10228
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10057
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
10002
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
9869
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...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
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
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...

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.