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

Accessing overridden base methods/functions

Hi!

Given the following example:
Expand|Select|Wrap|Line Numbers
  1. class Base
  2. {
  3. public:
  4.     void doSomething()
  5.     {
  6.         //Something...
  7.     }
  8. };
  9.  
  10. class Derived : public Base
  11. {
  12. public
  13.     void doSomething()
  14.     {
  15.         // Something else...
  16.         Base::doSomething();
  17.     }
  18. };
  19.  
  20. int main()
  21. {
  22.     Derived d;
  23.     d.doSomething();
  24.     return 0;
  25. }
  26.  
  27.  
Is there any reason why the Base::doSomething() shouldn't be invoked?
Jul 10 '07 #1
10 1709
Darryl
86
You forgot a colon after public, but, no, there is no reason why the base dosomething shouldn't be called.
Jul 10 '07 #2
Thank you for your swift reply, Darryl!

Do you know of any case that would make Base::doSomething invisible when overridden even though it is not a virtual func?

I am deriving from a precompiled object and the following example works as expected:

Expand|Select|Wrap|Line Numbers
  1. class Derived : public Base
  2. {
  3.     public:
  4.     void doSomethingExtra()
  5.     {
  6.         //something something...
  7.         Base::doSomething();
  8.     }
  9. };
  10.  
But...

Expand|Select|Wrap|Line Numbers
  1. class Derived : public Base
  2. {
  3.     public:
  4.     void doSomething()
  5.     {
  6.         //something something...
  7.         Base::doSomething();
  8.     }
  9. };
  10.  
...does not! No compiler or runtime error, just no execution of the Base-part.

Best regards
Jul 10 '07 #3
Darryl
86
I guess it depends on how you are accessing it. For example:

Expand|Select|Wrap|Line Numbers
  1. Base* pb = new Derived;
  2. pb->DoSomething(); // will call the base DoSomething and not the derived;
  3.  
Jul 11 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Do you know of any case that would make Base::doSomething invisible when overridden even though it is not a virtual func?
Yes. If you have a Derived::doSomething and you are using a Derived object, the Derived::doSomething hides, that is dominates, the Base::doSomething(). This is a big no-no becuse you cut out Base behavior in the Derived object. This forces you to call Base::doSomething inside Derived::doSomething.

The reason for the no-no is you can't tell inside Derived::doSomething whether a) you call Base::doSomething first (a pre-condition), b) you call Base::doSomething last ( a post-condition) or c) you do not call Base::doSomething at all. This is a three-way ambiguous scenario.

Note, this domination is based solely on the function name and does not consider the public/private/protected access specifiers or the function arguments. Example:

Expand|Select|Wrap|Line Numbers
  1. class Base
  2. {
  3. public:
  4.     void doSomething()
  5.     {
  6.         //Something...
  7.     }
  8. };
  9.  
  10. class Derived : public Base
  11. {
  12. public
  13.     void doSomething(int arg)
  14.     {
  15.         // Something else...
  16.     }
  17. };
  18.  
  19. int main()
  20. {
  21.     Derived d;
  22.     d.doSomething();  //ERROR doSomething requires int argument
  23.     return 0;
  24. }
  25.  
Jul 11 '07 #5
Darryl
86
The way to "unhide" the base function in your scenario is to use using

Expand|Select|Wrap|Line Numbers
  1. class Base
  2. {
  3. public:
  4.       void doSomething(void)
  5.      {
  6.           //Something...
  7.      }
  8. };
  9.  
  10. class Derived: public Base
  11. {
  12. public:
  13.  
  14.      using Base::doSomething;
  15.  
  16.       void doSomething(int arg)
  17.      {
  18.           // Something else...
  19.      }
  20. };
  21.  
  22. int main()
  23.  
  24. {
  25.      Derived d;
  26.      d.doSomething(); //OK now!!!
  27.      return 0;
  28.  
  29. }
Jul 11 '07 #6
Hi again :) Thank you both for your replies!

I have tried to use the
Expand|Select|Wrap|Line Numbers
  1.  using Base::doSomething; 
but I can't get it to solve the problem. The reason I am deriving this class is that it is a precompiled object that has a lot of calls to it, and I want to add some preconditions ( am I right? ) to the Base functions. In other words the Base functions is used as post-condition in the Derived. I have done this with success several times on other projects, but now it seems to behave differently.

I solved the problem by renaming all the functions in the Derived and rerouting to the Base class, although I had hoped my inheritance-plan would save me from that kind of work.
Jul 12 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
If I get this right, you just want to change how the base class works. Why not inherit privately?

Everything in the base class becomes private in the derived class leaving you free to write member functions on the derived class that can to anything you want. Some might just call base class functions, others might do something different, while others may add functionality.

Private inheritance is implementation inheritance. That is, you want the base class implementation but do not want to expose the base class interface.

This is a classic way of fixing broken classes that belong to third parties.
Jul 12 '07 #8
If I get this right, you just want to change how the base class works. Why not inherit privately?

Everything in the base class becomes private in the derived class leaving you free to write member functions on the derived class that can to anything you want. Some might just call base class functions, others might do something different, while others may add functionality.

Private inheritance is implementation inheritance. That is, you want the base class implementation but do not want to expose the base class interface.

This is a classic way of fixing broken classes that belong to third parties.
To me it sounded like he wanted to be able to still call the functions as he would fore the base class. for example if he has a base class object and wants it to do something he would do:

baseOb.doSomething();

now he just wants to alter what it does, so he makes a derived class to modify it with conditions so that he can do this:

derivedOb.doSomething();

and get the functionality he wants.

If he inherits privately he wouldn't be able to call the functions in his program. Basically he just wants to override the function, and also include a call to the base classes version.

What if you did something like this:

Expand|Select|Wrap|Line Numbers
  1. class Base
  2. {
  3. public:
  4.     void doSomething()
  5.     {
  6.         //Something...
  7.     }
  8. };
  9.  
  10. class Derived : public Base
  11. {
  12. public:
  13.     void doSomething()
  14.     {
  15.         // Something else...
  16.         Base *temp = this;
  17.         temp->doSomething();
  18.     }
  19. };
  20.  
  21. int main()
  22. {
  23.     Derived d;
  24.     d.doSomething();
  25.     return 0;
  26. }
This will create a pointer to the current object that thinks it's a Base object, which will then call the base classes version of the function. I tested it and it seems to work.

I think his main problem is that his original post, line 16 attempted to call the function like this: Base::doSomething(); which can't be done unless doSomething() is static. You need to invoke it through an object, and so you just need to treat the current object as a base object to get the base function.
Jul 12 '07 #9
Darryl
86
What compiler are you using? I just tried this code on MSVC++ 2005 and it works as expected:
Expand|Select|Wrap|Line Numbers
  1. include <iostream>
  2.  
  3. class base
  4. {
  5. public:
  6.      void doSomething()
  7.      {
  8.           std::cout << "Base\n";
  9.      }
  10. };
  11.  
  12. class derived : public base
  13. {
  14. public:
  15.      void doSomething()
  16.      {
  17.           base::doSomething();
  18.           std::cout << "Derived\n";
  19.      }
  20. };
  21. int main()
  22. {
  23. derived b;
  24. b.doSomething();
  25. }
Jul 12 '07 #10
What compiler are you using? I just tried this code on MSVC++ 2005 and it works as expected:
Expand|Select|Wrap|Line Numbers
  1. include <iostream>
  2.  
  3. class base
  4. {
  5. public:
  6.      void doSomething()
  7.      {
  8.           std::cout << "Base\n";
  9.      }
  10. };
  11.  
  12. class derived : public base
  13. {
  14. public:
  15.      void doSomething()
  16.      {
  17.           base::doSomething();
  18.           std::cout << "Derived\n";
  19.      }
  20. };
  21. int main()
  22. {
  23. derived b;
  24. b.doSomething();
  25. }
yeah, I didn't even try the original, but I did now, and it does work. But if it is a compiler problem, making a base class pointer to the current object to call the base class function should get around any compiler specific problems.
Jul 13 '07 #11

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

Similar topics

5
by: Istvan Albert | last post by:
Hello all, if I have this code: import sets class Foo: x = sets.Set() then pychecker says:
1
by: Bryan Ray | last post by:
I am trying to write an inheritance function, so I can call a base classes method that has been overridden in the derived class. I want to get rid of the ugly 'call()' syntax that would be used....
3
by: Ben | last post by:
Please help: I have a class called BALL and I have some classes that inherit from BALL: BASKETBALL, BASEBALL, SOCCERBALL, MINI_SOCCERBALL, etc. I want to be able to have a single object that...
4
by: Sean Connery | last post by:
I have a Microsoft UI Process Application Block that is controlling child forms in an MDI parent container. The views node in the app.config file has been set to stayOpen=false. Because there...
11
by: S. I. Becker | last post by:
Is it possible to determine if a function has been overridden by an object, when I have a pointer to that object as it's base class (which is abstract)? The reason I want to do this is that I want...
8
by: Allan Ebdrup | last post by:
I'm writing some code where I have have a class that implements 4 methods (class A) I only want to call these methods from the base class if they have been overridden in a sub class (Class B) I...
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
12
by: Ratko | last post by:
Hi all, I was wondering if something like this is possible. Can a base class somehow know if a certain method has been overridden by the subclass? I appreciate any ideas. Thanks, Ratko
1
by: nsphelt | last post by:
I am wondering if it is possible to access the underlying property of a base class within a derived that has overridden that property. I am using VB.NET and when I use the MyBase keyword to access...
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?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.