polymorphism, overloading...

D

deancoo

I'm having some trouble with a couple of classes I've created (base and
derived). I had defined a function in a derived class, and then overloaded
that function name in a class derived from the first derived class. Now I
need to store objects instantiated from these classes (excluding the base
class) in a container. The container is defined to hold objects of the base
class type. So far so good, however, when it comes time to use the above
described member functions, the compiler complains about them not being
defined. Fare enough, the prototype is missing from the base class. I'm
not sure how to approach the problem. I can't use a virtual override in the
base class because the functions have different parameters, so what can I do
short of redesigning the whole thing?

Thanks,
d
 
P

Phlip

deancoo said:
I'm having some trouble with a couple of classes I've created (base and
derived). I had defined a function in a derived class, and then overloaded
that function name in a class derived from the first derived class. Now I
need to store objects instantiated from these classes (excluding the base
class) in a container. The container is defined to hold objects of the base
class type. So far so good, however, when it comes time to use the above
described member functions, the compiler complains about them not being
defined. Fare enough, the prototype is missing from the base class. I'm
not sure how to approach the problem. I can't use a virtual override in the
base class because the functions have different parameters, so what can I do
short of redesigning the whole thing?

Can you post some code? That would let us suggest the minimum tweaks.

Do you have a 'virtual' method in the base class? Does the derived class
really override that method (with the same signature)?

And Google for "Polymorphic Array C++".
 
V

Victor Bazarov

deancoo said:
I'm having some trouble with a couple of classes I've created (base and
derived). I had defined a function in a derived class, and then overloaded
that function name in a class derived from the first derived class.

I think you're misusing the term "overloaded". In a derived class you
cannot usually overload any member functions from the base class, you can
override them, provided they are declared 'virtual'. Is that what you're
really doing?
Now I
need to store objects instantiated from these classes (excluding the base
class) in a container. The container is defined to hold objects of the base
class type. So far so good,

Not good at all. The container should be defined to hold pointers to base
class, not objects. If you attempt to store objects, you will get what is
known as "slicing".
however, when it comes time to use the above
described member functions, the compiler complains about them not being
defined. Fare enough, the prototype is missing from the base class. I'm
not sure how to approach the problem. I can't use a virtual override in the
base class because the functions have different parameters, so what can I do
short of redesigning the whole thing?

Nothing, really.

V
 
H

Howard

deancoo said:
I'm having some trouble with a couple of classes I've created (base and
derived). I had defined a function in a derived class, and then
overloaded that function name in a class derived from the first derived
class. Now I need to store objects instantiated from these classes
(excluding the base class) in a container. The container is defined to
hold objects of the base class type.

For polymorphism to work properly, you need to store pointers to base class
types, not the base class objects themselves. You may be doing that, but
your description above states that you're storing objects, not pointers.
So far so good, however, when it comes time to use the above described
member functions, the compiler complains about them not being defined.
Fare enough, the prototype is missing from the base class. I'm not sure
how to approach the problem. I can't use a virtual override in the base
class because the functions have different parameters, so what can I do
short of redesigning the whole thing?

Redesigning *might* be the way to go. If you've got different parameters,
then they're not the same functions, and giving them the same names in a
class hierarchy simply causes the derived classes' versions to "hide" the
base class versions.

One solution is to have identical parameters. One way to do that is to pass
a single parameter, which is itself an object (preferably a const reference,
if you're not changing the parameters themselves) which can hold all the
varieties of parameters that might be needed. Then, each override of the
virtual function can simply pick out the items from that parameter object
that it needs.

Another method (although ugly) is to identify the actual type of derived
class the object is, using RTTI or some other method, and use a cast to call
the function for that specific class. But as I said, that's ugly, and the
need to do so is what would point me towards a redesign of the whole thing.

Of course, if you gave more specific info on what you're doing, perhaps with
some code, we might be able to tell you more.

-Howard
 
D

deancoo

deancoo said:
I'm having some trouble with a couple of classes I've created (base and
derived). I had defined a function in a derived class, and then
overloaded that function name in a class derived from the first derived
class. Now I need to store objects instantiated from these classes
(excluding the base class) in a container. The container is defined to
hold objects of the base class type. So far so good, however, when it
comes time to use the above described member functions, the compiler
complains about them not being defined. Fare enough, the prototype is
missing from the base class. I'm not sure how to approach the problem. I
can't use a virtual override in the base class because the functions have
different parameters, so what can I do short of redesigning the whole
thing?

Thanks,
d

Yes, sorry I wasn't more clear, but I am storing pointers to the base
object.
 
D

deancoo

Howard said:
One solution is to have identical parameters. One way to do that is to
pass a single parameter, which is itself an object (preferably a const
reference, if you're not changing the parameters themselves) which can
hold all the varieties of parameters that might be needed. Then, each
override of the virtual function can simply pick out the items from that
parameter object that it needs.

If I was able to redefine the functions to all use the same parameter, would
this be the optimal or 'cleanest' solution? I can see the virtual
overridden solution working quite well.

d
 
H

Howard

deancoo said:
If I was able to redefine the functions to all use the same parameter,
would this be the optimal or 'cleanest' solution? I can see the virtual
overridden solution working quite well.

d

With no idea what your design is or what problems you're actually facing,
it's impossible for me to tell you what's cleanest. Optimal is even more
difficult to judge. But in my opinion, if you're using polymorphism, then
it should be used "right", and that means using virtual functions. Whether
that means you should use my above suggestion, I can't say. I just gave an
option, given the limited info I had available. It's possible you need to
redesign the system, but there's really no way I can tell from here.

-Howard
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top