Problem casting from a vector

D

DJ.precario

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[(int)dataTablesVector.size()-1];

this gives me the following error:

error C2440: 'type cast' : cannot convert from
'std::allocator<_Ty>::value_type' to 'DerivedClass'
with
[
_Ty=BaseClass *
]


Where am I going wrong?

Thanks.
 
R

Rolf Magnus

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)myVector[(int)dataTablesVector.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_type' 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?
 
P

Peter Julian

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[(int)dataTablesVector.size()-1];

this gives me the following error:

error C2440: 'type cast' : cannot convert from
'std::allocator<_Ty>::value_type' 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.
 
R

Rolf Magnus

Peter said:
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[(int)dataTablesVector.size()-1];

this gives me the following error:

error C2440: 'type cast' : cannot convert from
'std::allocator<_Ty>::value_type' 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.
 
P

Peter Julian

Rolf Magnus said:
Peter said:
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[(int)dataTablesVector.size()-1];

this gives me the following error:

error C2440: 'type cast' : cannot convert from
'std::allocator<_Ty>::value_type' 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.

Thats a valid point. However, just because a truck can hold a vector of cars
doesn't validate trying to cast an element in that vector into the container
truck. A truck can carry trucks, but a truck can't carry itself. Its the
logic i'm arguing, not the language's features.

There is a clear distinction between saying that a truck can carry a
reference to itself (the serial number) and converting a contained element
into this.

In the case of a container of 2D shapes, you could argue that the container
can hold other shape containers, or even specialized containers of 2D
shapes. I'm arguing that just because i can cast a specialized element into
the original container, it doesn't neccessarily make sense to do so.

The composite design pattern lets you treat both primitives and their
containers as uniform objects with respect to their behaviours (as in a
tree). The goal is to provide common behaviours throughout the forest
(pardon the analogy). The goal is not to falicitate casting a tree, or leaf
into the forest itself.

If i'm mistaken, let me know.
 

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

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top