Sign in to follow this  

[RESOLVED] Polymorphism (Oh such fun) - Overloading non-virtual methods

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What I have is a base class without any virtual methods, for I do not need any. What I want to do is have a child class derrive from that base class, and then overload some methods... However, I run into a problem: If I don't add scoping to the method call, it'll get confused (if the number of args are the same) and tell me there is an error in converting in intialization. Example:
class Base {
public:
 void func(int x) {
  cout << "OMG! Int\n";
 }
};

class Child : public Base {
public:
 void func(char *str) {
  cout << "OMG! String\n";
 }
};

//In Main
Child test;
//Attempt 1
test.func("Hello"); //Works fine
test.func(2); //Error.
//Attempt 2
test.func("Hello"); //Same
test.Base::func(2); //No error
Is there anything I can put in the "Child" declaration that can clear this up? However, please note that the overload's arguments will contain children of classes that are in the original method's arguments. Thinking over this some more, I'm thinking that simply using the scope as is will reduce possible errors in the future. [Edited by - deadimp on April 29, 2006 11:05:25 PM]

Share this post


Link to post
Share on other sites
You could pull the base member into the derived class' scope with a using declaration, by why are you overloading inherited member functions in the first place? That kind of thing always smells like a design error to me.

Share this post


Link to post
Share on other sites
I have a basic container class, and then I have an extended container class that 'contains' different objects (inhereted by the ones used by the basic container), using the same basic functionality, and then adds a few more operations.
I'm using this because using 'virtual' here won't apply since the arguments are different, and I don't want to repeat code over and over again (for there are going to be several 'extended' containers).

NOTE: Something I just realized (kind of sad)... When you haev a class derrive from a base, the base's methods can be separated by scoping, therefore you could define functions exactly as those defined in the base, and it would be legal (not polymorphic).

Share this post


Link to post
Share on other sites
When you declare a class member function with the same name as a non-virtual member function, you're hiding the base class' function (hence the using declaration).

Does your base container class have a default implementation? If not, then you should virtualize your functions (a template would be a better choice here for variant element types).

Share this post


Link to post
Share on other sites
What do you mean by default implementation?

As for virtual methods, I have thought it through some, and I have come to the conclusions that it wouldn't be as secure if I did use those, because these containers deal with object registration/unregistration, creation and destruction (not constructors or destructors) that are very specific to the container.
And templates, I had given that some thought before too, but I couldn't think of a solid, efficient solution.

Thanks for putting up with me so far [smile].

EDIT: Nypyren >> Sorry, I'm somewhat confused by what you're asking...

EDIT 2:
I'm overloading these methods in this 'new' container to impose restrictions on what kinds of classes the user (mainly me at this point) can register/unregister/etc. The other functions which I don't need to overload can stay like they are because the restrictions don't really apply since the container already has a 'filter' by the previous restrictions.

Share this post


Link to post
Share on other sites
It seems to me like Base shouldn't be a parent of Child, they're both children of a more general container class. You may need to provide more specifics. If you need to have containers that hold different types of elements, the easiest way would be to create a template. Remember, public inheritance means "is-a", and if your interfaces are different for each container, then that relationship doesn't hold true.

Share this post


Link to post
Share on other sites
Please note that the container don't contain entirely different classes. The base container class contains a base object class, and the extended container class contains an exetended object class (I think this may be the answer to Nypyren's question).
The base container class has some basic operations that deal with the base object class.
The extended container class has more advanced operations that deal with the extended object class. However, the same basic operations are carried out on the extended object class using the base container class, taking those extended objects as base objects.

(Good God, I think I lost count of 'extended' and 'base'...)

To make things more clear, I will attempt to make a simple inhertiance chart...
"X -> { ... }" means "X" is implemented by the "..." classes. "X(o)" means "X" contains the "o" classes. Line separations should be interpereted as the next item.

//Polymorphic
GameItem -> {
Instance -> { Object, Weapon } //'Instance' is not a container!!!
Background
Tile
}

//Non-polymorphic
Container(GameItem) -> {
InstanceContainer(Instance) -> { ObjectContainer(Object), WeaponContainer(Weapon), ... } //This is what I'm talking about right now
BgContainer(Background)
TileContainer(Tile)
}


EDIT: Also, implementation doesn't automatically imply polymorphism, but it can.

Well, there's the basis of my game engine too.

Share this post


Link to post
Share on other sites
So your base container contains base objects, while a derived container holds derived objects? Using polymorphism, the derived container need not hold anything; the base container already holds pointers to base objects. The derived container's only purpose would be to provide additional functionality to the base container, not base objects. In other words, you don't need a derived container to extend derived object functionality - the derived object already does that.

You initialize a game object (weapon, et al.), then place it into your base container. What additional functionality do these derived containers provide? (I question their necessity)

Share this post


Link to post
Share on other sites
Quote:
The derived container's only purpose would be to provide additional functionality to the base container

Exactly.
I need this derived container so that I can restrict what types of objects are passed to it without repeating massive (well, medium, more like) amounts of code.
I would use the special container so that I have a set list of certain objects rather that searching the base container for those certain objects. These 'certain objects' interact with each other and other objects differently than those other objects. To put it in a nutshell, making 'selective' iteration faster.
An example would be that in creating a weapon, I want it know it's creator so that I can keep track of who it came from. Using this information, I can decide who the weapon will hurt, and the source's reaction when the weapon hits or misses (if the source still validly exists).

Thanks!

EDIT: Another example is for team interaction. I don't want to cast and re-cast when I'm searching through a list of team objects to find out who is an enemy and friend. I want this extended container to do that for me.
The use of the extended container isn't as profound as it may seem.

Share this post


Link to post
Share on other sites
Actually, I have decided to simply stick with the scoping option, for it reduces possible mix-ups in the future.

Thanks to everyone for the help!

Share this post


Link to post
Share on other sites

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this