Topic: Denied overloading


Author: "Alexei S. Zakharov" <A.S.Zakharov@inp.nsk.su>
Date: 1999/04/19
Raw View
Hi,

Recently I hit one interesting problem related to function overloading in
derived class. Let's consider a following code:

class A
{
public:
    void dummy()
    {
        // ...
    }
};

class B : public A
{
public:
    void dummy( int a )
    {
        // ...
    }
}

int main()
{
    // ...
    A a;
    B b;
    a.dummy();     //  right
    b.dummy( 0 );  //  right
    b.dummy();     //  error: not such member function
    // ...
}

Why is there such error? Common sense says me that b.dummy() should call
a.dummy() because B is derived from A. By the way, this works with Java,
why does it refuse to work with C++?

Sincerely yours,
Alexei Zakharov.

mailto: A.S.Zakharov@inp.nsk.su
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 1999/04/19
Raw View
In article <Pine.SGI.3.95.990419000402.286448A-100000@Sky.inp.nsk.su>,
  "Alexei S. Zakharov" <A.S.Zakharov@inp.nsk.su> wrote:
[snip]
> int main()
> {
>     // ...
>     A a;
>     B b;
>     a.dummy();     //  right
>     b.dummy( 0 );  //  right
>     b.dummy();     //  error: not such member function
>     // ...
> }
>
> Why is there such error? Common sense says me that b.dummy() should call
> a.dummy() because B is derived from A. By the way, this works with Java,
> why does it refuse to work with C++?
Common sense says

a) you should check the C++ FAQ before posting (hint: look up "hiding rule")

b) C++ and Java are different languages.  If you want to write Java idioms,
use Java.

The C++ FAQ can be found at:
  http://www.cerfnet.com/~mpcline/c++-faq-lite/

--
Jim
I ignore all email from recruitment agencies.
Please do not send me email with questions - post here.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Patrick =?iso-8859-1?Q?M=E9rissert=2DCoffini=E8res?= <pamc@club-internet.fr>
Date: 1999/04/20
Raw View
Yes, this has been standard hiding behaviour in C++ for a very long time. It
ould also occur if A defined both

void dummy();
and
void dummy(int);

and you redefined only

void dummy(int);

in B. The compiler gives a wrning message. In my opinion it is quite
reasonable: it is not so frequent to have to "partially" redefine, or extend
a name that was defined in a base class, and it coul be a typing mistake.
But if you know what you are doing, and you want the derived class to make
available all he variant of the names, there is a way:

class B: public A {
public:
  using A::dummy;
  void dummy(int a){/*...*/}
};

This is part of the standard, and I think nowadays I would think all
compilers accept it.

Regards,

Patrick

"Alexei S. Zakharov" wrote:

> Hi,
>
> Recently I hit one interesting problem related to function overloading in
> derived class. Let's consider a following code:
>
> class A
> {
> public:
>     void dummy()
>     {
>         // ...
>     }
> };
>
> class B : public A
> {
> public:
>     void dummy( int a )
>     {
>     }
> }
>
> int main()
> {
>     A a;
>     B b;
>     a.dummy();     //  right
>     b.dummy( 0 );  //  right
>     b.dummy();     //  error: not such member function
> }
>
> Why is there such error? Common sense says me that b.dummy() should call
> a.dummy() because B is derived from A. By the way, this works with Java,
> why does it refuse to work with C++?
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]