overriding some of the methods with the same name yields an error

overriding some of the methods with the same name yields an error

Post by Oren » Fri, 09 Jun 2006 17:26:06


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.yqcomputer.com/ ]
 
 
 

overriding some of the methods with the same name yields an error

Post by uvts_cv » Fri, 09 Jun 2006 18:57:23


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.yqcomputer.com/ ]

 
 
 

overriding some of the methods with the same name yields an error

Post by Anders Dal » Fri, 09 Jun 2006 19:03:40


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.yqcomputer.com/ ]
 
 
 

overriding some of the methods with the same name yields an error

Post by Francis Gl » Fri, 09 Jun 2006 19:29:21

In article < XXXX@XXXXX.COM >, Oren
< XXXX@XXXXX.COM > writes
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.yqcomputer.com/
For project ideas and contributions: http://www.yqcomputer.com/

---
[ 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.yqcomputer.com/ ]
 
 
 

overriding some of the methods with the same name yields an error

Post by jdennet » Fri, 09 Jun 2006 23:58:18


Indeed; add "using B::foo;" to D's definition if you
want to bring in the name from the base class.


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.yqcomputer.com/ ]
 
 
 

overriding some of the methods with the same name yields an error

Post by jdennet » Sat, 10 Jun 2006 01:43:01


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.yqcomputer.com/ ]
 
 
 

overriding some of the methods with the same name yields an error

Post by Oren » Mon, 12 Jun 2006 18:16:14

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.yqcomputer.com/ ]