|
-
April 17th, 2004, 09:34 PM
#1
Inheritance problem
Hello
I'll describe my problem. I've got a vehicle class (cVehicle), and two 'heirs', car (cCar)and van (cVan). The vehicle class has atributes X and Y, car has C and van has V, and both heirs inherit X and Y.
i have a class called Vehiclefactory which has a method which recieves a string with the atributes and it must return a Cvehicle object. Then i must add it to a list.
Code:
CVehiculo* temp = Vehiclefactory.Create (" string with atributes");
VehicleList.Add (temp);
Inside Vehiclefactory.create function, if i get a string with a C, i must create a car, and if i get a string with a V i must create a van
Code:
if (c==C)
{
cCar * car = new cCcar(X,Y,C);
return aut;
}
if (c==V)
{
cVan * van = new cVan(X,Y,V);
return van;
}
but the function returns a cVehicle class, so i lost the car or van atribute.
Code:
class cVehiclefactory
{
private:
FILE *data;
public:
void read (char * filename);
cvehiculo * Create (char data[51]);
};
i really need the create function to return a Cvehicle class, but i need to actualle return a car or a van, to be able to see its atributes and then to add it to a list. these lines of code must be in the program
Code:
CVehiculo* temp = Vehiclefactory.Create (" string with atributes");
VehicleList.Add (temp);
hope you can help me!!
thanks
-
April 18th, 2004, 05:42 PM
#2
Well what type does the VehicleList.Add() function take as a parameter? I assume it's a cVehicle*, and in that case, it doesn't matter if the create function returns a cVehicle*, cCar* or a cVan*.
-
April 18th, 2004, 08:50 PM
#3
thanks
i solved the problem with static_cast
-
April 18th, 2004, 09:42 PM
#4
Re: thanks
Originally posted by kfaday
i solved the problem with static_cast
What problem? If Add() takes a cVechicle*, then there shouldn't have been a problem in the first place. Having to call static_cast<> is an indication that there is something wrong in your code, the design is not correct, or you're doing something that you're not telling us.
Regards,
Paul McKenzie
-
April 18th, 2004, 09:46 PM
#5
Re: Inheritance problem
Originally posted by kfaday
i really need the create function to return a Cvehicle class, but i need to actualle return a car or a van
That is what you're doing. You are returning a car and a van, but as a pointer to a car/van. What makes you thing you were not? Again, I don't see a problem.
Regards,
Paul McKenzie
-
April 19th, 2004, 09:17 AM
#6
that's what i've done:
i created a temp cvehicle object. I created it, then if it's a car or van, i use static_cast , and it works, i can insert a car or a van to the list
Code:
ccar *car;
cvan *van
cvehiculo* temp=new cvehicle();
temp=create ("string with atributes");
node=new CNode(); //to insert it in a list
if it's a car
car=static_cast<ccar *>(temp);
node->Setvehicle(car);
if it's a van
van=static_cast<cvan *>(temp);
node->Setvehicle(van);
-
April 19th, 2004, 09:32 AM
#7
Code:
cvehiculo* temp=new cvehicle();
temp=create ("string with atributes");
If I read your code correctly, the create() function does
a "new van" or "new car", so you do not do the "new"
before calling create(). As it is, you have a memory leak.
Code:
cvehiculo* temp; // no "new cvehicle()"
temp=create ("string with atributes");
-
April 19th, 2004, 09:43 AM
#8
!
thanks philip!!
you were right
-
April 19th, 2004, 09:48 AM
#9
Originally posted by kfaday
that's what i've done:
i created a temp cvehicle object. I created it, then if it's a car or van, i use static_cast , and it works, i can insert a car or a van to the list
Code:
ccar *car;
cvan *van
cvehiculo* temp=new cvehicle();
temp=create ("string with atributes");
node=new CNode(); //to insert it in a list
if it's a car
car=static_cast<ccar *>(temp);
node->Setvehicle(car);
if it's a van
van=static_cast<cvan *>(temp);
node->Setvehicle(van);
I really don't understand why you need to do any casting here. Assuming you only only have one CNode::SetVehicle function (no overloading), it will have to take a cvehiculo* as a parameter to be able to handle both ccar* and cvan* being passed to it. That in turn means that the entire casting procedure you are using now, is meaningless as the the pointers will be inserted as cvehiculo pointers anyway.
-
April 19th, 2004, 09:59 AM
#10
I still don't understand why you need to do this:
Code:
//if it's a car
car=static_cast<ccar *>(temp);
node->Setvehicle(car);
//if it's a van
van=static_cast<cvan *>(temp);
node->Setvehicle(van);
You never mentioned what argument Setvehicle takes. If it takes a cvehiculo*, then there is no need for this code at all. If it does not, then what you are doing is an error.
The static_cast tells the compiler to ignore that the pointers are not related, and go ahead and make a car a van. But my point is that the pointers are related (they are both derived from the same parent), therefore there is no need for the static_cast. What kind of pointer does Setvehicle take?
Also, what if you come up with 5 other types of vehicles? Are you going to write an if() statement to add these 5 vehicles? Your code breaks everything that polymorphism is supposed to cure, and that is to not worry about what each type of vehicle is.
To prove my point, the code below shows that you are doing something that you still don't want to show us:
Code:
class Vehicle
{
public:
virtual ~Vehicle() { }
virtual Vehicle *Create()const = 0;
};
void AddVehicle(Vehicle *pVehicle)
{
}
class Car : public Vehicle
{
public:
Vehicle *Create() const { return new Car; }
};
class Bus : public Vehicle
{
public:
Vehicle *Create() const { return new Bus; }
};
class Truck : public Vehicle
{
public:
Vehicle *Create() const { return new Truck; }
};
void CreateVehicle( const Vehicle& v )
{
Vehicle* pVehicle = v.Create();
// no casting
AddVehicle( pVehicle );
}
int main()
{
CreateVehicle( Car() );
CreateVehicle( Bus() );
CreateVehicle( Truck() );
}
So now if I come up with a HotRod class derived from Car(), I don't need to change any of my class code. All I need to do is call CreateVehicle(HotRod()) and everything works as expected.
Regards,
Paul McKenzie
-
April 19th, 2004, 09:59 AM
#11
you are right assmaster (and PAUL)
i used
node->Setvehicle(veh);
for all cases (van and car) and everything works fine.
thanks!
(Thanks for the big example Paul!!!)
i went through all that trouble, because i wanted to see car's or van's atributes there, by typing cout. I couldn't see them with veh.
but i don't need to display the objects atributes there, so static cast is useless there
Last edited by kfaday; April 19th, 2004 at 10:03 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|