A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » Why can't call Base::Method from Derived class ?

#1 2005-03-16 09:49 PM

From: Porto Alegre, Brasil
Registered: 2005-02-08
Posts: 47

Re: Why can't call Base::Method from Derived class ?

/* File start *****************************/

class Base
        bool func(void) {
           return func(1);    /* call Base::func(int) */

        virtual bool func(int i) = 0;

class Deriv : public Base
        bool func(int i) {      /* here we implement Base::funct(int) */
            return i == _i;

        int _i;

int main(){
        Deriv d;
        d.func();          /* this should call Base::func(void), isn't ? /*
        return 1;

/* File end *****************************/


#2 2005-03-17 03:58 PM

From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Why can't call Base::Method from Derived class ?

Indeed very weird, doesn't work with gcc 3.4.3 either. It works if you don't use overloading, or when casting d to Base first. I've no idea if the C++ standard allows for overloading across classes or not, or how's that supposed to be handled. Maybe it's a bug in gcc, if so look for known bugs on gcc's webpage and if it isn't listed post it as a new one. Then they'll probably tell you if it's supposed to work or not I quess.


#3 2005-03-22 11:51 AM

Anders Goude
From: Sweden
Registered: 2002-08-01
Posts: 6

Re: Why can't call Base::Method from Derived class ?

Actually, I don't think this is a bug. (tried it in another compiler than gcc, and it worked in the same way)
If you redefine a virtual function in a derived class, it hides all other functions in the base class with the same name, regardless if they take the same argument or not. Atleast, this is how the standard used to work.
The simple solution to this problem is to rename bool func(void) to something different and it will work.


#4 2005-03-30 05:56 PM

From: Sol 3
Registered: 2002-06-12
Posts: 911

Re: Why can't call Base::Method from Derived class ?

One of these should also work... and Anders' comment about the hiding rule is absolutely correct... though I'm gonna add that it doesn't matter if the function is virtual or not...  the same hiding occurs...

Try unhiding the func in base with the line:

using Base::func;

inside your Deriv class header or change the call d.func() to...

( ( Base * ) & d )->func();

Note you have to use the ptr since trying to cast to Base proper will fail because Base is abstract ( thanks to the pure virt func(...) )...

Last but not least you can specifically target the base class call by changing the  d.func() line to...


Having said that... all of the above is... the WRONG thing to do and potentially very bad things can happen even if you do get it to compile... look at your classes...  your func(void) in Base should have a different name AND the func(int) in Deriv should be virtual...

Think about it...

If you omit the first step then any override of the virtual function in Base is going to hide the non-virtual version... and make it innaccessible without jumping through hoops like using one of the "solutions" provided...

If you omit the second step you have a derived class that contains a pure virtual function ( due to the pure virtual that is in Base with the same signature ) which actually remains undefined on execution...  you are simply "hiding" this dangerous fact ...

Oh yeah, I still like typing long winded answers... lol. :-/


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)


  • Index
  • » C++
  • » Why can't call Base::Method from Derived class ?

Board footer

Powered by FluxBB