重定义不同基类的同名虚函数

http://ilewen.com/questions/6423

 

 

class I
{
public:
  virtual void foo() = 0;
};

class J
{
public:
  virtual void foo() = 0;
};

class C : public I, public J
{
public:
  virtual void I::foo() { cout << "I" << endl; }
  virtual void J::foo() { cout << "J" << endl; }
};

上面的代码编译和运行都没问题,现在我想把I::foo() 和 J::foo() 的定义移到类申明外,如下

class C : public I, public J
{
public:
  virtual void I::foo();
  virtual void J::foo();
};
void C::I::foo() { cout << "I" << endl; }
void C::J::foo() { cout << "J" << endl; }

编译报错

1>.\main.cpp(38) : error C2509: 'foo' : member function not declared in 'C'
1> .\main.cpp(31) : see declaration of 'C'
1>.\main.cpp(39) : error C2509: 'foo' : member function not declared in 'C'
1> .\main.cpp(31) : see declaration of 'C'

请问要把那两个函数的定义移出来应该怎么做呢?

 

FUNCTION显示函数名发现I::foo和J::foo的函数名都是C::foo,在其他地方恐怕没有办法区别这两个函数了:

//DECLARATION
class C : public I, public J
{
public:
  virtual void I::foo() { fooI(); }
  virtual void J::foo() { fooJ(); }

  void fooI();
  void fooJ();
};

//IMPLEMENTATION
void C::fooI() { cout << "I" << endl; }
void C::fooJ() { cout << "J" << endl; }

 

 

 

http://stackoverflow.com/questions/5481356/overriding-qualified-virtual-methods

 

I have C++ class with multiple parents; each parent defines a function with a common name but a different purpose:

class BaseA 
{ 
    virtual void myFunc();  // does some task 
}; 
class BaseB 
{ 
    virtual void myFunc();  // does some other task 
}; 
class Derived : public BaseA, public BaseB; 

If that was it, I would have no problem - I could resolve the ambiguity it with a using statement, and I could choose which one to call using the base class names and the scope resolution operator.

Unfortunately, the derived class needs to override them both:

class Derived : public BaseA, public BaseB 
{ 
    virtual void BaseA::myFunc(); // Derived needs to change the way both tasks are done 
    virtual void BaseB::myFunc(); 
} 

This doesn't work, not because it introduces a new ambiguity (although it may), but because

"error C3240: 'myFunc' : must be a non-overloaded abstract member function of 'BaseA'"

"error C2838: illegal qualified name in member declaration"

Under different circumstances I might just rename the methods, or make them pure virtual as the compiler suggests. However, the class hierarchy structure and a number of external issues make the first option extremely difficult, and the second impossible.

Does anyone have a suggestion? Why are qualifiers only allowed for pure virtual methods? Is there any way to simultaneously override virtual methods and resolve ambiguities?

 

 

 

I think this is a workaround:

class BaseA 
{ 
protected: 
    virtual void myFunc();  // does some task 
}; 
class BaseB 
{ 
protected: 
    virtual void myFunc();  // does some other task 
}; 
class ShimA : virtual BaseA 
{ 
    virtual void myFunc() { myFuncA(); } 
protected: 
    virtual void myFuncA() { BaseA::myFunc(); } 
}; 
class ShimB : virtual BaseB 
{ 
    virtual void myFunc() { myFuncB(); } 
protected: 
    virtual void myFuncB() { BaseB::myFunc(); } 
}; 
class Derived : public virtual BaseA, public virtual BaseB, protected ShimA, protected ShimB 
{ 
     virtual void myFuncA() {} 
     virtual void myFuncB() {} 
}; 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值