Hi,
With your current struct you cannot avoid the creation of the Vehicle in the
parent class. Note that usually this has not further effect than just
another instance to be GCed.
Now, a more elegant solution could be achieve by changing a little the
constructors.
Person:
protected Person( Vehicle v){ fVehicle = v;}
public Person():this( new Vehicle){}
Child:
public Child(): base( new Bycicle() ){}
Note that if this would have been in any other case except in the
constructors a virtual method will solve the situation.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Julius Fuchs" <ju**********@yahoo.dewrote in message
news:1a*****************************@40tude.net...
Hi,
if I create a Person, a Vehicle is created; that's fine.
The problem is that if I create a Child the constuctor of Person is called
before and a Vehicle is created then a Bike is created and the variable is
overwritten. How can I prevent this?
class Person
{
protected Vehicle fVehicle;
public Person()
{
fVehicle = new Vehicle();
}
}
class Child : Person
{
public Child()
{
fVehicle = new Bike();
}
}
class Vehicle
{}
class Bike : Vehicle
{}