开发者

C++ overloaded function issue

Why does the compiler not find the base class function signature? Changing foo( a1 ) to B::foo( a1 ) works.

Code:

class A1 ;
class A2 ;

class B
{
public:
   void foo( A1* a1 ) { a1 = 0 ; }
} ;

class C : public B
{
public:
   void foo( A2* /*a2*/ )
   {
      A1* a1 = 0 ;
      foo( a1 ) ;
   }
} ;

int main()
{
   A2* a2 = 0 ;
   C c ;
   c.foo( 开发者_如何学运维a2 ) ;
   return 0 ;
}

Compiler error (VS2008):

error C2664: 'C::foo' : cannot convert parameter 1 from 'A1 *' to 'A2 *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


The name C::foo shadows the name B::foo. Once the compiler finds the matching foo in class C, it stops searching any further.

You can resolve your problem by adding:

using B::foo;

to the body of class C, or by renaming the function in class B.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜