Topic: Inheritance and overloading question


Author: Philip Staite <pstaite@home.net>
Date: 1999/11/19
Raw View
See the C++ FAQ at http://www.cerfnet.com/~mpcline/C%2b%2b-FAQs-Lite/
and look up "Hiding Rule"...  You've got a classic example.

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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: Sreeram <psreeram@cas.org>
Date: 1999/11/19
Raw View
Sean Shubin wrote:
>
> I had thought that this was valid C++ code according to the standard,
> yet it does not compile in either MSVC++6.0 or C++Builder4.  Seems that
> the overloaded function is somehow hiding the original function, even
> though they have different signatures (but the same name).  Aren't you

For overloading the function should have same scope,
same name and different signature. func1 is not defined
in the same scope and hence the functions are not
overloaded. Both compilers are correct in hiding func1.

Sree

> supposed to be able to use a derived class in any way its base class can
> be used?  Is there something in the standard I am missing, or are both
> these compilers non compliant in reporting an error.
>
> class Base
> {
> public:
>     void func1(int a){}
> };
>
> class Derived : public Base
> {
> public:
>     void func1(int b,int c){}
> };
>
> int
> main(int argc,char** argv)
> {
>     Derived d;
>     d.func1(3); //ERROR: 'func1': function does not take 1 parameters
>     return 0;
> }

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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: dark__panda@my-deja.com
Date: 1999/11/19
Raw View
In article <3831EC8B.187C4C4@integtech.com>,
  Sean Shubin <sean.shubin@integtech.com> wrote:
> I had thought that this was valid C++ code according to the standard,
> yet it does not compile in either MSVC++6.0 or C++Builder4.  Seems
that
> the overloaded function is somehow hiding the original function, even
> though they have different signatures (but the same name).  Aren't you
> supposed to be able to use a derived class in any way its base class
> can be used?  Is there something in the standard I am missing, or are
> both these compilers non compliant in reporting an error.
>
> class Base
> {
> public:
>     void func1(int a){}
> };
>
> class Derived : public Base
> {
> public:
>     void func1(int b,int c){}
> };
>
> int
> main(int argc,char** argv)
> {
>     Derived d;
>     d.func1(3); //ERROR: 'func1': function does not take 1 parameters
>     return 0;
> }

Try d.Base::func1(3); instead.


Sent via Deja.com http://www.deja.com/
Before you buy.

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/11/19
Raw View
Horst Kraemer wrote:
>
> On 17 Nov 1999 12:49:39 -0500, Sean Shubin <sean.shubin@integtech.com>
> wrote:
>
> > I had thought that this was valid C++ code according to the standard,
> > yet it does not compile in either MSVC++6.0 or C++Builder4.  Seems that
> > the overloaded function is somehow hiding the original function, even
> > though they have different signatures (but the same name).  Aren't you
> > supposed to be able to use a derived class in any way its base class can
> > be used?  Is there something in the standard I am missing, or are both
> > these compilers non compliant in reporting an error.
>
> Note that this isn't _overloading_ but _overriding_. Overloading takes
> place when functions are declared in the same scope. In this case
> Base::func1 is declared in the scope of Base and Derived::func1
> overrides it.

No, overriding is what happens if a virtual function of the base
class is "replaced" by a derived class version, so that calls
through an lvalue with static type Base still call the derived
class' version on derived class objects.

What happens here is called "hiding": The derived class version
hides the base class version.

Note that those are not mutually exclusive: If the base class
has overloaded virtual functions, a derived class function
overriding one of them will also hide the other one.

>
> > class Base
> > {
> > public:
> >     void func1(int a){}
> > };
> >
> > class Derived : public Base
> > {
> > public:

        using Base::func1; // avoid hiding of Base::func1

> >     void func1(int b,int 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              ]






Author: Sean Shubin <sean.shubin@integtech.com>
Date: 1999/11/17
Raw View
I had thought that this was valid C++ code according to the standard,
yet it does not compile in either MSVC++6.0 or C++Builder4.  Seems that
the overloaded function is somehow hiding the original function, even
though they have different signatures (but the same name).  Aren't you
supposed to be able to use a derived class in any way its base class can
be used?  Is there something in the standard I am missing, or are both
these compilers non compliant in reporting an error.

class Base
{
public:
    void func1(int a){}
};

class Derived : public Base
{
public:
    void func1(int b,int c){}
};

int
main(int argc,char** argv)
{
    Derived d;
    d.func1(3); //ERROR: 'func1': function does not take 1 parameters
    return 0;
}
---
[ 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: Marc Lepage <mlepage@molecularmining.com>
Date: 1999/11/18
Raw View
Sean Shubin wrote:
>
> I had thought that this was valid C++ code according to the standard,
> yet it does not compile in either MSVC++6.0 or C++Builder4.  Seems that
> the overloaded function is somehow hiding the original function, even
> though they have different signatures (but the same name).  Aren't you
> supposed to be able to use a derived class in any way its base class can
> be used?  Is there something in the standard I am missing, or are both
> these compilers non compliant in reporting an error.
>
> class Base
> {
> public:
>     void func1(int a){}
> };
>
> class Derived : public Base
> {
> public:
>     void func1(int b,int c){}
> };
>
> int
> main(int argc,char** argv)
> {
>     Derived d;
>     d.func1(3); //ERROR: 'func1': function does not take 1 parameters
>     return 0;
> }

This is the hiding problem. Overload resolution is not applied across different
scopes. [Stroustrup, 1997] 7.4.

As stated in [Stroustrup, 1997] 7.5: "Declaring a name in a nested scope so that
the name hides a declaration of the same name in an outer scope is error prone."

Using-declarations can ease this problem. [Stroustrup, 1997] 15.2.2.

--
Marc Lepage
Software Developer
Molecular Mining Corporation
http://www.molecularmining.com/

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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: horst.kraemer@t-online.de (Horst Kraemer)
Date: 1999/11/18
Raw View
On 17 Nov 1999 12:49:39 -0500, Sean Shubin <sean.shubin@integtech.com>
wrote:

> I had thought that this was valid C++ code according to the standard,
> yet it does not compile in either MSVC++6.0 or C++Builder4.  Seems that
> the overloaded function is somehow hiding the original function, even
> though they have different signatures (but the same name).  Aren't you
> supposed to be able to use a derived class in any way its base class can
> be used?  Is there something in the standard I am missing, or are both
> these compilers non compliant in reporting an error.

Note that this isn't _overloading_ but _overriding_. Overloading takes
place when functions are declared in the same scope. In this case
Base::func1 is declared in the scope of Base and Derived::func1
overrides it.

> class Base
> {
> public:
>     void func1(int a){}
> };
>
> class Derived : public Base
> {
> public:
>     void func1(int b,int c){}
> };
>
> int
> main(int argc,char** argv)
> {
>     Derived d;
>     d.func1(3); //ERROR: 'func1': function does not take 1 parameters

      d.Base::func1(3);

would do the job.

Regards
Horst


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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: "Leon" <lf14514n@pace.edu>
Date: 1999/11/18
Raw View
> class Derived : public Base
> {
> public:
you can introduce the functions from Base in this scope like this:
using Base::func1;
>     void func1(int b,int c){}
> };
    Derived::func1 hides  the Base::func1because it is declared in outer
scope and redeclared with same name in derived class, which is inner scope.
    You can find a chapter on this in "C++ FAQs" 2nd Ed. (Mixing Overloading
with Inheritance)



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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              ]