考虑以下继承层次结构:
class A{};
class X1:A{};
class X2:A{};
class Y1:X1{};
class Y2:X2{};
class Z1:X1{};
class Z2:X2{};
X层中可能有更多类,如类X3:A {}; ...
每个X? class有两个派生类Y?和Z?。
我需要添加一个方法,所有Y1 ... Yn实例都相同,所有Z1 ... Zn实例都使用另一种方法实现。
这是由A类中的方法调用的非公共方法。
有没有一种优雅的方法来完成这项任务?
答案 0 :(得分:2)
Delegation模式可能会回答您的问题
此示例是上面复杂Java示例的C ++版本。由于C ++没有接口构造,因此纯虚拟类扮演相同的角色。优点和缺点与Java示例大致相同。
#include <iostream>
using namespace std;
class I {
public:
virtual void f() = 0;
virtual void g() = 0;
virtual ~I() {}
};
class A : public I {
public:
void f() { cout << "A: doing f()" << endl; }
void g() { cout << "A: doing g()" << endl; }
~A() { cout << "A: cleaning up." << endl; }
};
class B : public I {
public:
void f() { cout << "B: doing f()" << endl; }
void g() { cout << "B: doing g()" << endl; }
~B() { cout << "B: cleaning up." << endl; }
};
class C : public I {
public:
// construction/destruction
C() : i( new A() ) { }
virtual ~C() { delete i; }
private:
// delegation
I* i;
public:
void f() { i->f(); }
void g() { i->g(); }
// normal attributes
void toA() { delete i; i = new A(); }
void toB() { delete i; i = new B(); }
};
int main() {
C c;
c.f(); //A: doing f()
c.g(); //A: doing g()
c.toB(); //A: cleaning up.
c.f(); //B: doing f()
c.g(); //B: doing g()
}
答案 1 :(得分:2)
如果公共继承是可接受的,那么您可以定义父类,并在派生类中指向这些类:
#include <iostream>
using namespace std;
class A
{
// A cannot be abstract - it must be able to invoke a
// "non-public method" whose implementation varies based on Y vs Z
public:
virtual void myFun()=0;
void callMyFun(){myFun();}
};
class Y
{
public:
void myFun(){cout<< "myFun from Y superclass" << endl;}
};
class Z
{
public:
void myFun(){cout<< "myFun from Z superclass" << endl;}
};
class X1:public A{};
class X2:public A{};
class Y1:X1{
Y* y;
void myFun(){y->myFun();}
};
class Y2:public X2{
Y* y;
void myFun(){y->myFun();}
};
class Z1:public X1{
Z* z;
void myFun(){z->myFun();}
};
class Z2:public X2{
Z* z;
void myFun(){z->myFun();}
};
int main()
{
Y2 myY2;
myY2.callMyFun(); // myFun from Y superclass
}
答案 2 :(得分:0)
你可能想看看PIMPL成语。函数实现封装在Y * s和Z * s委托给的类中。