473,748 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing overridden base methods/functions

3 New Member
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::doSomethi ng() shouldn't be invoked?
Jul 10 '07 #1
10 1734
Darryl
86 New Member
You forgot a colon after public, but, no, there is no reason why the base dosomething shouldn't be called.
Jul 10 '07 #2
herlands
3 New Member
Thank you for your swift reply, Darryl!

Do you know of any case that would make Base::doSomethi ng 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 New Member
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 Recognized Expert Moderator Expert
Do you know of any case that would make Base::doSomethi ng invisible when overridden even though it is not a virtual func?
Yes. If you have a Derived::doSome thing and you are using a Derived object, the Derived::doSome thing hides, that is dominates, the Base::doSomethi ng(). This is a big no-no becuse you cut out Base behavior in the Derived object. This forces you to call Base::doSomethi ng inside Derived::doSome thing.

The reason for the no-no is you can't tell inside Derived::doSome thing whether a) you call Base::doSomethi ng first (a pre-condition), b) you call Base::doSomethi ng last ( a post-condition) or c) you do not call Base::doSomethi ng 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 New Member
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
herlands
3 New Member
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 Recognized Expert Moderator Expert
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
phiefer3
67 New Member
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.doSometh ing();

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.doSom ething();

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::doSomethi ng(); 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 New Member
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

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

Similar topics

5
1469
by: Istvan Albert | last post by:
Hello all, if I have this code: import sets class Foo: x = sets.Set() then pychecker says:
1
1738
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. Ideally I would like to call the method using syntax such as: object.base.method() The idea is to use an object ('base') to provide the base classes properties and proxy methods to the real methods stored in '_class' - see the function below....
3
1383
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 keeps track of every ball, so I have a vector of type BALL*, where I keep track of all of the different balls no matter what inherited class they are.
4
1861
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 are a series of data entry forms using ONLY the numeric key pad, certain keys on this pad have been configured to behave as "function keys". In order for this to happen, I have implemented the IMessageFilter interface to intercept messages in the...
11
2758
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 to call this function if it has been overridden, and not if it hasn't, e.g class CBase { public: virtual long mightBeOverridden(int i) { return 0; } // not a pure virtual function, since sub-classes should not have to define it
8
5232
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 guess I could have some properties that specify wether to call the methods, but I would like to call them automatically when they are overridden, how do I do this using reflection? Class A { private void methodX()
2
2637
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 class BaseClass {
12
4904
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
1506
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 the property of the base class, it accesses the overridden property instead. Specifically, my class is inheriting the TextBox class and I am overriding the Text property to have it display something different based on the state of my class. I was...
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9333
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.