Re: function lookup by Doug
Doug
Mon Jun 25 05:57:59 CDT 2007
On Mon, 25 Jun 2007 09:48:58 +0200, "Alessandro Vergani"
<avergani@insignis.it> wrote:
>class A
>{
> virtual void Test(void*)
> {
> }
>};
>
>class B : public A
>{
> virtual void Test(void*, void*)
> {
> }
>};
>
>void main()
>{
> B test;
> test.Test(0);
>}
>
>this code doesn't compile:
>test.cpp(18) : error C2660: 'B::Test' : function does not take 1 arguments
>
>Why?
>
>Test(void*) is a member of the base class of B, so why the compiler can't
>find it?
>
>Thank you very much.
Overloading doesn't span scopes. The function B::Test has a different
signature from A::Test and thus doesn't override it. Instead, because
overloading doesn't span scopes, it is said to hide A::Test. I wouldn't
necessarily recommend it, but you can bring the declaration of A::Test into
B with a using-declaration:
class B : public A
{
using A::Test;
virtual void Test(void*, void*)
{
}
};
Then your example will work. That said, it's usually best to avoid
overloading virtual functions, because a derived class that overrides fewer
than all of them hides the rest. It would be somewhat weird to only need to
override some of them, and usually, you can define a set of non-virtual
overloads and one (typically protected) virtual function they all defer to.
--
Doug Harrison
Visual C++ MVP