C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Stupid function name reuse

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
davidmontgomery
Guest





PostPosted: Fri Oct 22, 2004 5:21 pm    Post subject: Stupid function name reuse Reply with quote



Hi,

I'm trying to untangle some code that looks, in part,
like this:

class Base
{
public:
void Caller() { Function( "Whence?" ); }
void Function( char * szArg = NULL ) {}
};

class Derived : public Base
{
public:
void Function( char * szArg = NULL, int nArg = 0 ) {}
};

int main()
{
D d;
d.Caller();

return 0;
}

Will Caller() call Base::Function() or Derived::Function()?
Why?

Thanks much,
David Montgomery


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Victor Bazarov
Guest





PostPosted: Sat Oct 23, 2004 2:14 pm    Post subject: Re: Stupid function name reuse Reply with quote



davidmontgomery wrote:
Quote:
I'm trying to untangle some code that looks, in part,
like this:

class Base
{
public:
void Caller() { Function( "Whence?" ); }
void Function( char * szArg = NULL ) {}
};

class Derived : public Base
{
public:
void Function( char * szArg = NULL, int nArg = 0 ) {}
};

int main()
{
D d;
d.Caller();

return 0;
}

Will Caller() call Base::Function() or Derived::Function()?
Why?

The 'Caller' should call Base::Function, unfortunately. I say
'unfortunately' because "Whence?" is an array of _constant_ chars
and the argument to Base::Function is a pointer to non-const char.

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John M. Dlugosz
Guest





PostPosted: Sun Oct 24, 2004 4:03 am    Post subject: Re: Stupid function name reuse Reply with quote



"davidmontgomery" <davidmontgomery (AT) netzero (DOT) com> wrote

Quote:
class Derived : public Base
{
public:
void Function( char * szArg = NULL, int nArg = 0 ) {}
};

int main()
{
D d; // assuming you meant ==> Derived d;
d.Caller();

Will Caller() call Base::Function() or Derived::Function()?
Why?

It will call Derived::Function. The type of d is Derived, and there
is one function named Function in it. That's all it can possibly
call.

The Base::Function is hidden by the use of the name in the derived
class; they will not overload.

--John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
rishi
Guest





PostPosted: Sun Oct 24, 2004 4:11 am    Post subject: Re: Stupid function name reuse Reply with quote

Hi,
names in derived class hide those in the base class. In this case only
derived class "Function" is visible.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Frank Birbacher
Guest





PostPosted: Mon Oct 25, 2004 4:41 pm    Post subject: Re: Stupid function name reuse Reply with quote

Hi!

John M. Dlugosz wrote:
Quote:
Will Caller() call Base::Function() or Derived::Function()?
Why?

It will call Derived::Function. The type of d is Derived, and there
is one function named Function in it. That's all it can possibly
call.

The Base::Function is hidden by the use of the name in the derived
class; they will not overload.

That is not true. The Base class does not know about Derived. So
Base::Caller will always call Base::Function, despite of function
hiding. If Base::Function was declared 'virtual', then
Base::Caller would still call Base::Function through virtual
dispatch, because Derived::Function would not override
Base::Function due to incompatible parameters lists.

Frank


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
davidmontgomery
Guest





PostPosted: Mon Oct 25, 2004 7:47 pm    Post subject: Re: Stupid function name reuse Reply with quote

Thanks to all for their responses.

Both g++ (3.3.3) and MSVC++ 7.1 call Base::Function(), as Victor
suggested.

(Of course I should have written Derived d, not D d. Thanks John.
I should have avoided the distraction of "Whence?"'s const nature
by making Function()s take a const char * as the first argument;
that wasn't the issue I meant to get at.)

John Dlugosz wrote:
Quote:
It will call Derived::Function. The type of d is Derived, and there
is one function named Function in it. That's all it can possibly
call.

rishi wrote:
Quote:
names in derived class hide those in the base class. In this case
only derived class "Function" is visible.

The call to Function is from Base::Caller(). Certainly within
Base::Caller() the function Base::Function is visible.

I guess what convinces me that g++, msvc and Victor are correct
is thinking about separate compilation. Base::Function() isn't
virtual. So the call from Base::Caller() to Function() can be
(I guess must be) compiled directly without any indirection.
The fact that we later link a Base.obj with a Derived.obj shouldn't
change what gets called here.

John and rishi (or anyone else), if you believe that both gcc
and msvc are wrong on this, please post a follow-up.
Thanks,
David Montgomery


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Tokyo Tomy
Guest





PostPosted: Tue Oct 26, 2004 4:17 am    Post subject: Re: Stupid function name reuse Reply with quote

"davidmontgomery" <davidmontgomery (AT) netzero (DOT) com> wrote

Quote:
...

class Base
{
public:
void Caller() { Function( "Whence?" ); }
void Function( char * szArg = NULL ) {}
};

class Derived : public Base
{
public:
void Function( char * szArg = NULL, int nArg = 0 ) {}
};

int main()
{
D d;
d.Caller();

return 0;
}

Will Caller() call Base::Function() or Derived::Function()?
Why?
...

IMHO, since Caller() is a member function of Base class, the Caller
will call the Function in the base class.

If you add a Caller() to Derived class, the Caller call the Function
in the derived class.
In this case,
(1) if you remove void Function( char * szArg = NULL, int nArg = 0 )
{} , the Caller will call Function in the base class, and
(2) if you replace void Function( char * szArg = NULL, int nArg = 0 )
{} with void Function() {}, then an error will be issued, because void
Function() hide the Function in the base class.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Alberto Barbati
Guest





PostPosted: Tue Oct 26, 2004 1:29 pm    Post subject: Re: Stupid function name reuse Reply with quote

rishi wrote:
Quote:
Hi,
names in derived class hide those in the base class. In this case only
derived class "Function" is visible.


Quite an imprecise answer.

In the scope of class Derived, Derived::Function is visible,
Base::Function having been hidden.

In the scope of function main, Derived::Function is visible, because
Base::Function is hidden and the static type of the variable d is Derived.

(notice that Base::Function could still be called in both scopes if
explicitly qualified, but that's not our case)

However, in the scope of class Base, *only* Base::Function is visible.
Such function is not virtual and even if it were virtual
Derived::Function would not override Base::Function because the
parameter list differs.

As the call to Function is made from inside Base::Caller, that is from
inside the scope of class Base, Base::Derived will thus be called.

Alberto

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.