Topic: overloading base class functions


Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/11/14
Raw View
vandevod@cs.rpi.edu (David Vandevoorde) writes:

>>>>>> "FH" == Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> writes:
>[...]
>FH> Yes, this is now allowed in C++, with slightly different syntax:
>
>FH>  struct base { int f(); };
>FH>  struct derived : base { int f(int); using base::f(); };
>FH>          ^^^^^
>
>Are you positive?

Oops, no I'm not ;-)

>I thought the using declaration could only declare
>`names' and could only do so if the name were not already a member of
>the scope in which the using declaration appears (which I think would
>mean that the using declaration has to come before the member function
>declaration).

You're absolutely right.  It should be

  struct base { int f(); };
  struct derived : base { using base::f; int f(int); };

Thanks for correcting my mistake.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1996/11/14
Raw View
David Vandevoorde writes:

>>>>>> "FH" == Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> writes:
> [...]
FH> Yes, this is now allowed in C++, with slightly different syntax:

FH> struct base { int f(); };
FH> struct derived : base { int f(int); using base::f(); };

The using declaration should be "using base::f", without parentheses.

> Are you positive? I thought the using declaration could only declare
> `names' and could only do so if the name were not already a member of
> the scope in which the using declaration appears (which I think would
> mean that the using declaration has to come before the member function
> declaration).

The current DWP says that if the signature of a function defined in
the current class scope would conflich with any function imported by
the using clause, the imported one is overridden.  This is only valid
in classes, not in namespaces or blocks.  See [namespace.udecl]
paragraphs 12 and 13.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br
Universidade Estadual de Campinas, SP, Brasil
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/11/14
Raw View
>>>>> "AO" == Alexandre Oliva <oliva@dcc.unicamp.br> writes:
AO> David Vandevoorde writes:
>> [...] I thought the using declaration could only
>> declare `names' and could only do so if the name were not already a
>> member of the scope in which the using declaration appears (which I
>> think would mean that the using declaration has to come before the
>> member function declaration).

AO> The current DWP says that if the signature of a function defined
AO> in the current class scope would conflich with any function
AO> imported by the using clause, the imported one is overridden.
AO> This is only valid in classes, not in namespaces or blocks.  See
AO> [namespace.udecl] paragraphs 12 and 13.

But also note paragraph 1: `A name specified in a using-declaration
in a class or namespace scope shall not already be a member of that
scope.' I suppose `member of' here does not include ``brought in
with a using-declaration.''

 Daveed
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Dominic North <Dominic.North@ping.be>
Date: 1996/11/16
Raw View
In article <orwwvpx48y.fsf@dcc.unicamp.br> on 13 Nov 96 18:19:20 GMT
Alexandre Oliva <oliva@dcc.unicamp.br> wrote:
<< Actually, this is valid as of Sept'96 DWP:

  7.3.3  The using declaration [namespace.udecl]

3 [snip]
          struct B {
                  void f(char);
//[snip]
          };
          struct D : B {
                  using B::f;
                  void f(int) { f('c'); } // calls B::f(char)
//[snip]
          };
>>

What happens if you have two functions f in the base class?

struct B
{

    void f(char c);
    void f(const string s);

};

struct D : public B
{

    void f(int i);

};

Would adding "using B::f" make both f(char) and f(const string) visible?

Dominic
Bruxelles, Fri, 15 Nov 1996 10:48 +0100
also cis:106136,2400
using Virtual Access 3.52 build 159c (32-bit)
on WinNT build 1057 (3.51)
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/11/16
Raw View
Dominic North <Dominic.North@ping.be> writes:

>What happens if you have two functions f in the base class?
>
>struct B {
>    void f(char c);
>    void f(const string s);
>};
>
>struct D : public B {
>    void f(int i);
>};
>
>Would adding "using B::f" make both f(char) and f(const string) visible?

Yes, that is exactly what happens.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: jfc@mit.edu (John Carr)
Date: 1996/11/13
Raw View
Often I want to be able to overload a base class function in a derived
class, but C++ name lookup rules say that the functions in the derived
class hide the base class functions.  I would like to be able to say:

 struct base { int f(); };
 struct derived : base { int f(int); base::f(); };

so that the base class f(void) is visible in the derived class.

The ARM (11.3) prohibits this but doesn't explain to my satisfaction
why such a feature would be bad.

1. Has the C++ committee considered allowing this feature?

2. Why is it bad?

--
    John Carr (jfc@mit.edu)
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/11/13
Raw View
jfc@mit.edu (John Carr) writes:

>I would like to be able to say:
>
> struct base { int f(); };
> struct derived : base { int f(int); base::f(); };
>
>so that the base class f(void) is visible in the derived class.
[...]
>1. Has the C++ committee considered allowing this feature?

Yes, this is now allowed in C++, with slightly different syntax:

 struct base { int f(); };
 struct derived : base { int f(int); using base::f(); };
         ^^^^^

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1996/11/13
Raw View
John Carr writes:

> Often I want to be able to overload a base class function in a derived
> class, but C++ name lookup rules say that the functions in the derived
> class hide the base class functions.  I would like to be able to say:

>  struct base { int f(); };
>  struct derived : base { int f(int); base::f(); };

> so that the base class f(void) is visible in the derived class.

Actually, this is valid as of Sept'96 DWP:

  7.3.3  The using declaration                         [namespace.udecl]

3 [snip]
          struct B {
                  void f(char);
//[snip]
          };
          struct D : B {
                  using B::f;
                  void f(int) { f('c'); } // calls B::f(char)
//[snip]
          };

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br
Universidade Estadual de Campinas, SP, Brasil
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/11/14
Raw View
>>>>> "FH" == Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> writes:
[...]
FH> Yes, this is now allowed in C++, with slightly different syntax:

FH>  struct base { int f(); };
FH>  struct derived : base { int f(int); using base::f(); };
FH>          ^^^^^


Are you positive? I thought the using declaration could only declare
`names' and could only do so if the name were not already a member of
the scope in which the using declaration appears (which I think would
mean that the using declaration has to come before the member function
declaration).

 Daveed
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]