Connecting Tech Pros Worldwide Help | Site Map
Reply
 
LinkBack Thread Tools Search this Thread
  #1  
Old August 28th, 2008, 02:57 PM
Newbie
 
Join Date: Jul 2008
Posts: 12
Default Overloading a method in a child class invalidates the parent version of the method

Good day all,

I am trying to overload (change the argument list) the method of a parent class within the child class. When I do that, the parent class version of the method stops being "declared". I am very surprised by this and am wondering if it's a compiler bug or within the C++ specs. If it is part of the specs, anyone know why it's like that?

Here's some sample code:
Expand|Select|Wrap|Line Numbers
  1. class Parent
  2. {
  3. public:
  4.     void Test() {}
  5. };
  6.  
  7. class Child : public Parent
  8. {
  9. public:
  10.     int Test(int Value) {return(Value);}
  11. };
  12.  
  13. int main(int argn, char *argc[])
  14. {
  15.     Child a;
  16.     a.Test();
  17. ]
  18.  
Which gives the error:
Expand|Select|Wrap|Line Numbers
  1. Error    1    error C2660: 'Child::Test' : function does not take 0 arguments    e:\nexgen\tests\test\test\main.cpp    45
  2.  
I don't see why this wouldn't be allowed, it's not like they only differed by return type: there's no ambiguity.

Thanks guys!
J-O
Reply
  #2  
Old August 29th, 2008, 04:01 AM
Familiar Sight
 
Join Date: Mar 2007
Posts: 133
Default

I am not certain why the overloaded function hides the base class function in cases like this, but it does. I'm sure there is a reason.

If you need to resolve the problem, you can do this:
Expand|Select|Wrap|Line Numbers
  1. class Child : public Parent
  2. {
  3. public:
  4.   using Parent::Test;
  5.   int Test(int Value) {return(Value);}
  6. };
Reply
  #3  
Old August 29th, 2008, 07:12 PM
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Age: 68
Posts: 4,928
Default

Research the Rule of Dominance.

Wirthin a given scope, the local variable dominates a variable outside that scope.
[code]
int a = 10;
void AFunction()
{
int a = 5;
cout << a; //you see 5
}

The same is true with class methods. Whenever you have a derived class method, that method dominates. The donimation is based on the method name only and not the arguments. Therefore, the derived class hides the base class method.
This is not good because your derrived objects can't use the base class method unless you code:
Expand|Select|Wrap|Line Numbers
  1. void Base::AMethod(int x)
  2. {
  3.  
  4. }
  5. void Derived::AMethod(int a, int b))
  6. {
  7.        Base::AMethod(b); //recover base class logic
  8.  
  9. }
  10.  
The problem is in whether the call to Base::AMethod is a pre-condition or a post-condition. That is, do you do it before or after the Derived class logic. Or both places? Now you have ambiguity and that spells poor design.

That means you cannot overload methods between classes. Inside the same class is OK but once you change scope, the dominance rule kicks in.

With an override (same method name and same arguments), you have exactly the same situation. But when you do this, it is presumed the override is done on a virtual method. This will correctly build the VTBL for the derived object. Otherwise, the derived method will still hide the base method and you lose base class functionality in the derived object.
Reply
  #4  
Old September 2nd, 2008, 07:24 PM
Newbie
 
Join Date: Jul 2008
Posts: 12
Default

Thanks guys.

Weaknessforcats, you answer is quite clear and complete, thank you very much.
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 204,687 network members.
Post your question now . . .
It's fast and it's free

Popular Articles