This is a multi-part message in MIME format.

------=_NextPart_000_0008_01C4A53A.DFC9D850
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

The 4th line in my main method in the code below does not compile.
Isn't there a way to overload a base class method in the derived class =
in C++?
I know C#, java and VB.NET allow this.
TIA
Sankar Nemani
class B
{
public:
void foo(){}
=20
};

class D: public B
{
public:
void foo(int i){}
};

void main()
{
B *b =3D new B();
D *d =3D new D();
b->foo();
d->foo(); //does not compile
}
------=_NextPart_000_0008_01C4A53A.DFC9D850
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.3790.186" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>The 4th line in my main method in the =
code below=20
does not compile.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Isn't there a way to overload a base =
class method=20
in the derived class in C++?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I know C#, java&nbsp;and VB.NET allow=20
this.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>TIA</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Sankar Nemani</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>class B<BR>{<BR>public:<BR>&nbsp;void=20
foo(){}<BR>&nbsp;<BR>};</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>class D: public =
B<BR>{<BR>public:<BR>&nbsp;void=20
foo(int i){}<BR>};</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>void main()<BR>{<BR>&nbsp;B *b =3D new=20
B();<BR>&nbsp;D *d =3D new=20
D();<BR>&nbsp;b-&gt;foo();<BR>&nbsp;<STRONG>d-&gt;foo(); //does not=20
compile<BR></STRONG>}</FONT></DIV></BODY></HTML>

------=_NextPart_000_0008_01C4A53A.DFC9D850--

Re: method hiding by Doug

Doug
Tue Sep 28 11:16:57 CDT 2004

Sankar Nemani wrote:

>The 4th line in my main method in the code below does not compile.
>Isn't there a way to overload a base class method in the derived class in C++?
>I know C#, java and VB.NET allow this.
>TIA
>Sankar Nemani
>class B
>{
>public:
> void foo(){}
>
>};
>
>class D: public B
>{
>public:
> void foo(int i){}
>};
>
>void main()
>{
> B *b = new B();
> D *d = new D();
> b->foo();
> d->foo(); //does not compile
>}

You need to employ a using-declaration:

class D : public B
{
public:

// Bring all B::foo into the scope of D.
using B::foo;

void foo(int i){}
};

--
Doug Harrison
Microsoft MVP - Visual C++

Re: method hiding by Sankar

Sankar
Tue Sep 28 13:14:37 CDT 2004

Thanks Doug.
Is the behavior same if the the foo in class B was marked virtual? It seems
like the using declaration brings down all the foos that are declared in
class B if there were more than one. Can some foos be virtual and others
non-virtual? Is there a selective way of bringing down only a particular
foo?
By multiple foos I mean multiple methods that have the same name "foo" with
different signatures.


"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:qg3jl0tq5shera3ima47pabuc9ncsguig0@4ax.com...
> Sankar Nemani wrote:
>
> >The 4th line in my main method in the code below does not compile.
> >Isn't there a way to overload a base class method in the derived class in
C++?
> >I know C#, java and VB.NET allow this.
> >TIA
> >Sankar Nemani
> >class B
> >{
> >public:
> > void foo(){}
> >
> >};
> >
> >class D: public B
> >{
> >public:
> > void foo(int i){}
> >};
> >
> >void main()
> >{
> > B *b = new B();
> > D *d = new D();
> > b->foo();
> > d->foo(); //does not compile
> >}
>
> You need to employ a using-declaration:
>
> class D : public B
> {
> public:
>
> // Bring all B::foo into the scope of D.
> using B::foo;
>
> void foo(int i){}
> };
>
> --
> Doug Harrison
> Microsoft MVP - Visual C++



Re: method hiding by Doug

Doug
Tue Sep 28 14:13:13 CDT 2004

Sankar Nemani wrote:

>Thanks Doug.
>Is the behavior same if the the foo in class B was marked virtual? It seems
>like the using declaration brings down all the foos that are declared in
>class B if there were more than one. Can some foos be virtual and others
>non-virtual?

Whether or not the functions are virtual is irrelevant; it works the same
either way.

>Is there a selective way of bringing down only a particular
>foo?

No. You can, however, write forwarding functions to achieve the same effect,
e.g.

void DerivedClass::BaseClassFunction()
{
BaseClass::BaseClassFunction();
}

>By multiple foos I mean multiple methods that have the same name "foo" with
>different signatures.

Here's something you might find surprising. Even if a base class declares a
virtual function private, a derived class can still override it. It can't
call it, of course, but it can still override it.

--
Doug Harrison
Microsoft MVP - Visual C++

Re: method hiding by Sankar

Sankar
Tue Sep 28 15:58:48 CDT 2004

Thank you.

"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:4odjl01urns9bdvjc566b90qqe95sljo68@4ax.com...
> Sankar Nemani wrote:
>
> >Thanks Doug.
> >Is the behavior same if the the foo in class B was marked virtual? It
seems
> >like the using declaration brings down all the foos that are declared in
> >class B if there were more than one. Can some foos be virtual and others
> >non-virtual?
>
> Whether or not the functions are virtual is irrelevant; it works the same
> either way.
>
> >Is there a selective way of bringing down only a particular
> >foo?
>
> No. You can, however, write forwarding functions to achieve the same
effect,
> e.g.
>
> void DerivedClass::BaseClassFunction()
> {
> BaseClass::BaseClassFunction();
> }
>
> >By multiple foos I mean multiple methods that have the same name "foo"
with
> >different signatures.
>
> Here's something you might find surprising. Even if a base class declares
a
> virtual function private, a derived class can still override it. It can't
> call it, of course, but it can still override it.
>
> --
> Doug Harrison
> Microsoft MVP - Visual C++