473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

general arrays reuse-inheritance question

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.
Jul 23 '05 #1
3 1263

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-

Jul 23 '05 #2
"Shezan Baig" <sh************@gmail.com> 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" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 23 '05 #3

Nicolas Pavlidis wrote:
"Shezan Baig" <sh************@gmail.com> 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-

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DPfan | last post by:
What's exactly the meaning of "code reuse" in C++? Why such kind of reuse have more advantages over the counterpart in other language like in C? How is "code reuse" realized in C++? By...
2
by: Ross Micheals | last post by:
All I have some general .NET questions that I'm looking for some help with. Some of these questions (like the first) are ones that I've seen various conflicting information on, or questions that...
6
by: patrick t music-images dt nl | last post by:
Hi, I'm evaluating Visual Studio and the .NET concept for his company, which is currently using Borland C++ Builder. Now I tried to create components/controls etc. in .NET and I can reuse it...
7
by: Felix Kater | last post by:
Hi, when I need to execute a general clean-up procedure (inside of a function) just before the function returns -- how do I do that when there are several returns spread over the whole function?...
0
by: Leonardo Francalanci | last post by:
I wrote a function to sum arrays. It works, but I had to cast the data pointer to int64 (because my arrays are 'int8'): int64* ptr1 = ARR_DATA_PTR(v1); What if I want to write a more general...
32
by: santosh | last post by:
In following code char str = "asdf" ; str = 's' ; is possible. But char *str = "asdf" ; str = 's' ; is an run-time error. What i understand is *str = "asdf" is stored in Read-only-Memory....
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
19
by: jacob navia | last post by:
There is an interesting discussion running in Slashdot now, about code reuse. The thema of the discussion is here: < quote > Susan Elliot Sim asks: "In the science fiction novel, 'A Deepness...
0
by: andymconline | last post by:
Hello all, I have an existing Oracle database application that requires a "simple" front-end (do these things ever turn out to be simple?). There is an existing API written which I would like to...
9
by: andreyvul | last post by:
I'm trying to do the following: typedef struct { char *bar; char **baz; } foo; const foo fop = { { "foo", { "bar", "baz", "bax" } }, { "goo", { "car", "cdr", "cfr" } }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.