473,396 Members | 2,068 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,396 software developers and data experts.

Overloading a method in a child class invalidates the parent version of the method

12
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
Aug 28 '08 #1
3 3509
scruggsy
147 100+
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. };
Aug 29 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Aug 29 '08 #3
Sirmont
12
Thanks guys.

Weaknessforcats, you answer is quite clear and complete, thank you very much.
Sep 2 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Joe | last post by:
Hi, Can I pass a "generic" class pointer as an argument to a function? For instance say classA and B are both derived from Z. { int iType =1;
4
by: ad | last post by:
base only can call the parent 's method The result of the example below is I am Parent I am Child How can we call the grandparent's method in a inhreited class, I want the result is I am...
15
by: Pohihihi | last post by:
How can I overload abstract type method in child class? e.g. public abstract void BaseMethod() { // do something } // in child class
3
by: Adam Nielsen | last post by:
Hi everyone, I've run into yet another quirk with templates, which IMHO is a somewhat limiting feature of the language. It seems that if you inherit multiple classes, and those classes have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.