Topic: overriding some of the methods with the same name yields an error
Author: "Oren" <oren.pub@gmail.com>
Date: Sun, 11 Jun 2006 09:16:14 CST Raw View
Thanks.
Francis, my main reason for publishing this message in this group is
the type of question, not "how can I handle this error" but "why is it
defined this way and what is the logic behind it". The answer I was
looking for is much like Anders response. I'm sorry if it is still the
wrong group.
You all helped me very much and I thank you all.
Oren
---
[ 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://www.comeaucomputing.com/csc/faq.html ]
Author: "Oren" <oren.pub@gmail.com>
Date: Thu, 8 Jun 2006 08:26:06 CST Raw View
Hi
There is a question I'm trying to solve for some time without any
success:
When a base class has several methods with the same name (overloaded)
and a derived class overrides one or some of the methods all other (not
overridden) methods are hidden as well.
for example:
#include <iostream>
using namespace std;
class B
{
public:
virtual void foo(const char* arg)
{cout<<"B::foo(const char*):"<<arg<<endl;}
virtual void foo(int arg)
{cout<<"B::foo(int):"<<arg<<endl;}
};
class D :public B
{
public:
virtual void foo(int arg)
{cout<<"D::foo(int):"<<arg<<endl;}
};
int main()
{
D* d = new D();
d->foo("hello");
d->foo(3);
delete d;
return 0;
}
this example refuses to compile on account of the "hello" char[6] not
able to be converted to an int. I tried it on g++ and VC6 and both did
the same so I guess it's not a compiler bug but rather a standard rule
or direction.
BTW when d's type is replaced to a B* the program runs OK.
my question is why am I seeing this behavior and what is the logic
behind this?
Thanks
Oren
---
[ 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://www.comeaucomputing.com/csc/faq.html ]
Author: jdennett@acm.org (James Dennett)
Date: Thu, 8 Jun 2006 14:58:18 GMT Raw View
Oren wrote:
> Hi
> There is a question I'm trying to solve for some time without any
> success:
> When a base class has several methods with the same name (overloaded)
> and a derived class overrides one or some of the methods all other (not
> overridden) methods are hidden as well.
> for example:
>
> #include <iostream>
> using namespace std;
>
> class B
> {
> public:
> virtual void foo(const char* arg)
> {cout<<"B::foo(const char*):"<<arg<<endl;}
>
> virtual void foo(int arg)
> {cout<<"B::foo(int):"<<arg<<endl;}
> };
>
> class D :public B
> {
> public:
> virtual void foo(int arg)
> {cout<<"D::foo(int):"<<arg<<endl;}
> };
>
> int main()
> {
> D* d = new D();
> d->foo("hello");
> d->foo(3);
> delete d;
> return 0;
> }
>
> this example refuses to compile on account of the "hello" char[6] not
> able to be converted to an int. I tried it on g++ and VC6 and both did
> the same so I guess it's not a compiler bug but rather a standard rule
> or direction.
Indeed; add "using B::foo;" to D's definition if you
want to bring in the name from the base class.
> BTW when d's type is replaced to a B* the program runs OK.
> my question is why am I seeing this behavior and what is the logic
> behind this?
Name lookup is separate from overload resolution (and
precedes it). This keeps the rules simple and consistent,
though it's not the only possible solution. I believe it
may also have been through a desire to stop changes in
(possibly non-virtual) base class functions affecting
users of derived classes (though I'm not sure I agree
with that philosophy).
-- James
---
[ 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://www.comeaucomputing.com/csc/faq.html ]
Author: uvts_cvs@yahoo.com
Date: Thu, 8 Jun 2006 09:57:23 CST Raw View
Oren wrote:
> class D :public B
> {
> public:
> virtual void foo(int arg)
> {cout<<"D::foo(int):"<<arg<<endl;}
> };
try adding "using B::foo;" to D class to tell the compiler to use
symbol foo declared in B.
---
[ 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://www.comeaucomputing.com/csc/faq.html ]
Author: "Anders Dalvander" <anders.dalvander@gmail.com>
Date: Thu, 8 Jun 2006 10:03:40 CST Raw View
Oren wrote:
> my question is why am I seeing this behavior and what is the logic
> behind this?
I've also asked this question before and got the following answer:
C++ standard section 10.2:
"A member name f in one sub-object B hides a member name f in
sub-object A if A is a base class of sub-object B."
This is done to avoid possible confusion, if you provide a function in
a direved class, you probably expect that function to be called, and it
could lead to errors if a base class version is called unexpectedly.
Do like this instead:
class D : public B
{
public:
using B::foo; // Bring B::foo instances into the derived class scope.
virtual void foo(int arg)
{cout<<"D::foo(int):"<<arg<<endl;}
};
---
[ 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://www.comeaucomputing.com/csc/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: Thu, 8 Jun 2006 10:29:21 CST Raw View
In article <1149764160.967774.160560@j55g2000cwa.googlegroups.com>, Oren
<oren.pub@gmail.com> writes
>Hi
>There is a question I'm trying to solve for some time without any
>success:
>When a base class has several methods with the same name (overloaded)
>and a derived class overrides one or some of the methods all other (not
>overridden) methods are hidden as well.
>for example:
This type of question should be posted to comp.lang.c++.moderated where
you would be told about class scope using declarations, which were
designed for exactly this kind of problem.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
---
[ 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://www.comeaucomputing.com/csc/faq.html ]
Author: jdennett@acm.org (James Dennett)
Date: Thu, 8 Jun 2006 16:43:01 GMT Raw View
Francis Glassborow wrote:
> In article <1149764160.967774.160560@j55g2000cwa.googlegroups.com>, Oren
> <oren.pub@gmail.com> writes
>> Hi
>> There is a question I'm trying to solve for some time without any
>> success:
>> When a base class has several methods with the same name (overloaded)
>> and a derived class overrides one or some of the methods all other (not
>> overridden) methods are hidden as well.
>> for example:
> This type of question should be posted to comp.lang.c++.moderated where
> you would be told about class scope using declarations, which were
> designed for exactly this kind of problem.
For the part of the post you quoted, certainly clcm would
have been a better forum. However, the original post also
said "my question is why am I seeing this behavior and
what is the logic behind this?" is topical for comp.std.c++,
as it's about the rationale behind the design and evolution
of C++.
-- James
---
[ 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://www.comeaucomputing.com/csc/faq.html ]