473,382 Members | 1,424 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,382 software developers and data experts.

polymorphism, overloading...

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
Jul 23 '05 #1
7 1352
deancoo wrote:
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++".

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 23 '05 #2
deancoo wrote:
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
Jul 23 '05 #3

"deancoo" <s2*******@yahoo.ca> wrote in message
news:sdDZd.48612$gJ3.15025@clgrps13...
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

Jul 23 '05 #4
"deancoo" <s2*******@yahoo.ca> wrote in message
news:sdDZd.48612$gJ3.15025@clgrps13...
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.
Jul 23 '05 #5

"Howard" <al*****@hotmail.com> wrote in message
news:r_*********************@bgtnsc05-news.ops.worldnet.att.net...

"deancoo" <s2*******@yahoo.ca> wrote in message
news:sdDZd.48612$gJ3.15025@clgrps13...

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
Jul 23 '05 #6
downcasting is also an option instead of virtual function.

"deancoo" <s2*******@yahoo.ca> wrote in message
news:08GZd.42133$KI2.21474@clgrps12...

"Howard" <al*****@hotmail.com> wrote in message
news:r_*********************@bgtnsc05-news.ops.worldnet.att.net...

"deancoo" <s2*******@yahoo.ca> wrote in message
news:sdDZd.48612$gJ3.15025@clgrps13...

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

Jul 23 '05 #7

"deancoo" <s2*******@yahoo.ca> wrote in message
news:08GZd.42133$KI2.21474@clgrps12...

"Howard" <al*****@hotmail.com> wrote in message
news:r_*********************@bgtnsc05-news.ops.worldnet.att.net...

"deancoo" <s2*******@yahoo.ca> wrote in message
news:sdDZd.48612$gJ3.15025@clgrps13...

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


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

Jul 23 '05 #8

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

Similar topics

13
by: Gurtaj | last post by:
Hello! I am working with PHP and MySQL in a proyect with some partners of our university, and we are thinking about building some classes and use inheritance and polymorphism. The problem is...
7
by: rashkatsa | last post by:
Hi all ! I have written a little module called 'polymorph' that allows to call different methods of a class that have the same name but that have not the same number of arguments. *args are...
10
by: Danny Ni | last post by:
Hi, I was asked a question in a recent interview, where in ASP.Net is polymorphism used? I got differrent answers from different people, One said ToString() method because it is working for...
3
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming ...
11
by: richard pickworth | last post by:
Can anyone explain polymorphism?(very simply). thanks richard
8
by: Locia | last post by:
What are the techniques used to implement parametric polymorphism in C++? A Polymorphic method can be virtual?
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
2
by: glen stark | last post by:
I have a templated function template<class T> void update_E(T& updateable) { .... } What I would like to do is store a vector of updateable objects, and for each of them call the update...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.