general arrays reuse-inheritance question

Discussion in 'C++' started by danra, Feb 2, 2005.

  1. danra

    danra Guest

    Hi,

    I have a question which seems to me pretty basic, unfortunately I
    can't seem to figure it out.

    Let's say I have an abstract base class Vehicle and classes Car and
    Truck which derive from it.

    Let's say I have abstract base class A which contains a list of
    vehicles, and I define a method to insert and handle a vehicle into
    the list.

    Then I want to define class B which contains a list of cars, and I
    want to reuse A's method for handling a new vehicle.

    Here's the code:

    class A
    {
    public:
    A();
    virtual ~A() = 0;

    protected:
    //appends vehicle to m_vehicleList
    virtual void InsertVehicle(Vehicle* vehicle);

    Vehicle* m_vehicleList;
    }

    class B : public A
    {
    public:
    B();
    virtual ~B();

    //appends car to m_vehicleList
    virtual void InsertCar(Car* car);
    }

    B::InsertCar obviously makes use of A::InsertVehicle, which inserts
    the car into m_vehicleList.
    The problem is, B has a list of vehicles instead of a list of cars,
    which would present a problem the moment I wanted to write a method
    which makes use of the "Car-ness" of the cars in the list.

    A possible solution seems dropping m_vehicleList and adding a
    parameter to InsertVehicle:

    class A
    {
    public:
    A();
    virtual ~A() = 0;

    protected:
    //appends vehicle to vehicleList
    virtual void InsertVehicle(Vehicle* vehicle, Vehicle**
    vehicleList);
    }

    class B : public A
    {
    public:
    B();
    virtual ~B();

    //appends car to m_carList
    virtual void InsertCar(Car* car);

    protected:
    Car* m_carList;
    }

    And in InsertCat invoke InsertVehicle(car,&m_carList). Unfortunately
    this wouldn't work, and rightly so, since "a parking lot of cars
    is-not-a parking lot of vehicles".

    So the question is - how can I reuse code so I won't have to write a
    seperate InsertCar, InsertTruck etc. for every derived class of A?

    Thank you very much.
     
    danra, Feb 2, 2005
    #1
    1. Advertising

  2. danra

    Shezan Baig Guest

    danra wrote:
    > Hi,
    >
    > I have a question which seems to me pretty basic, unfortunately I
    > can't seem to figure it out.
    >
    > Let's say I have an abstract base class Vehicle and classes Car and
    > Truck which derive from it.
    >
    > Let's say I have abstract base class A which contains a list of
    > vehicles, and I define a method to insert and handle a vehicle into
    > the list.
    >
    > Then I want to define class B which contains a list of cars, and I
    > want to reuse A's method for handling a new vehicle.
    >
    > Here's the code:
    >
    > class A
    > {
    > public:
    > A();
    > virtual ~A() = 0;
    >
    > protected:
    > //appends vehicle to m_vehicleList
    > virtual void InsertVehicle(Vehicle* vehicle);
    >
    > Vehicle* m_vehicleList;
    > }
    >
    > class B : public A
    > {
    > public:
    > B();
    > virtual ~B();
    >
    > //appends car to m_vehicleList
    > virtual void InsertCar(Car* car);
    > }
    >
    > B::InsertCar obviously makes use of A::InsertVehicle, which inserts
    > the car into m_vehicleList.
    > The problem is, B has a list of vehicles instead of a list of cars,
    > which would present a problem the moment I wanted to write a method
    > which makes use of the "Car-ness" of the cars in the list.
    >
    > A possible solution seems dropping m_vehicleList and adding a
    > parameter to InsertVehicle:
    >
    > class A
    > {
    > public:
    > A();
    > virtual ~A() = 0;
    >
    > protected:
    > //appends vehicle to vehicleList
    > virtual void InsertVehicle(Vehicle* vehicle, Vehicle**
    > vehicleList);
    > }
    >
    > class B : public A
    > {
    > public:
    > B();
    > virtual ~B();
    >
    > //appends car to m_carList
    > virtual void InsertCar(Car* car);
    >
    > protected:
    > Car* m_carList;
    > }
    >
    > And in InsertCat invoke InsertVehicle(car,&m_carList). Unfortunately
    > this wouldn't work, and rightly so, since "a parking lot of cars
    > is-not-a parking lot of vehicles".
    >
    > So the question is - how can I reuse code so I won't have to write a
    > seperate InsertCar, InsertTruck etc. for every derived class of A?
    >
    > Thank you very much.


    The original InsertVehicles() seemed *much* cleaner to me. And it is
    probably the way to go. The only change I would make is to use a
    vector of pointers instead of just a pointer. There is nothing wrong
    with having a vector of pointers to base classes.

    .. std::vector<Vehicle*> m_vehicleList;

    What you want to do with this vector is another thing. Do you want to
    perform some operations just on cars? Think about how you would do it
    in a real parking lot. You would go to each vehicle, check if it a
    car. If so, then do something with that car. You can do this in C++
    as well:

    .. for (m_vehicleList::iterator it = m_vehicleList.begin();
    .. it != m_vehicleList.end(); ++it) {
    .. // Check if it is a car
    .. Car *pCar = dynamic_cast<Car*>(*it);
    .. if (pCar) {
    .. pCar->doSomething();
    .. }
    .. }

    Hope this helps,
    -shez-
     
    Shezan Baig, Feb 2, 2005
    #2
    1. Advertising

  3. "Shezan Baig" <> writes:

    [...]

    > The original InsertVehicles() seemed *much* cleaner to me. And it is
    > probably the way to go. The only change I would make is to use a
    > vector of pointers instead of just a pointer. There is nothing wrong
    > with having a vector of pointers to base classes.
    >
    > . std::vector<Vehicle*> m_vehicleList;


    I'd use here boost::shared_ptr:

    std::vector<boost::shared_ptr<Vehicle> > m_vehicleList;

    So noone have to worry about destoying the pointers stored by
    std::vector correctly.

    Kind regards,
    Nicolas
    --
    | Nicolas Pavlidis | Elvis Presly: |\ |__ |
    | Student of SE & KM | "Into the goto" | \|__| |
    | | ICQ #320057056 | |
    |-------------------University of Technology, Graz----------------|
     
    Nicolas Pavlidis, Feb 2, 2005
    #3
  4. danra

    Shezan Baig Guest

    Nicolas Pavlidis wrote:
    > "Shezan Baig" <> writes:
    >
    > [...]
    >
    > > The original InsertVehicles() seemed *much* cleaner to me. And it

    is
    > > probably the way to go. The only change I would make is to use a
    > > vector of pointers instead of just a pointer. There is nothing

    wrong
    > > with having a vector of pointers to base classes.
    > >
    > > . std::vector<Vehicle*> m_vehicleList;

    >
    > I'd use here boost::shared_ptr:
    >
    > std::vector<boost::shared_ptr<Vehicle> > m_vehicleList;
    >
    > So noone have to worry about destoying the pointers stored by
    > std::vector correctly.
    >


    But should a parking lot destroy cars? :)
    Depends who owns them, I guess.

    -shez-
     
    Shezan Baig, Feb 2, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    5
    Views:
    562
    Steve C. Orr [MVP, MCSD]
    May 17, 2005
  2. Hylander

    To reuse or not to reuse....

    Hylander, Feb 26, 2004, in forum: Java
    Replies:
    0
    Views:
    441
    Hylander
    Feb 26, 2004
  3. code reuse and design reuse

    , Feb 7, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,066
    Malcolm
    Feb 12, 2006
  4. jacob navia

    To reuse or not to reuse

    jacob navia, Nov 5, 2006, in forum: C Programming
    Replies:
    19
    Views:
    568
    Dave Thompson
    Dec 18, 2006
  5. Bart Simpson
    Replies:
    6
    Views:
    558
    Dave Rahardja
    May 6, 2007
Loading...

Share This Page