如果存在这样的事情,我会遇到一种“三角继承”问题。基本上,我有一个定义接口的抽象基类,以及一个定义该接口的 part 的实现的策略类,如下所示:
class SystemA : public System, public SomethingImplementation {/*...*/};
class SystemB : public System, public SomethingImplementation {/*...*/};
class SystemC : public System, { /* with a custom implementation ... */ };
然后我为不同的情况提供了几种接口实现。在某些情况下,我想使用简单的实现,而在其他情况下,我想要自定义它们
59:13: error: cannot declare variable 'a' to be of abstract type 'SystemA'
22:7: note: because the following virtual functions are pure within 'SystemA':
6:18: note: virtual void System::doSomething()
7:18: note: virtual bool System::somethingDone() const
60:13: error: cannot declare variable 'b' to be of abstract type 'SystemB'
31:7: note: because the following virtual functions are pure within 'SystemB':
6:18: note: virtual void System::doSomething()
7:18: note: virtual bool System::somethingDone() const
63:38: error: invalid new-expression of abstract class type 'SystemA'
我无法弄清楚SystemA或SystemB类的细节可能会使实现工作
https://blog.kapeli.com/dash-3-dot-4
解决方案1
使用语句,在这种情况下似乎没有做任何事情。
SomethingInterface
解决方案2
添加额外的接口类。创建Interface
类,并从SomethingImplementation
继承SomethingInterface
和$('a.some-link-class').click(function(e) {
e.preventDefault();
if ($(window).width() < 1000) window.location = 'http://small-screen-url/';
else window.location = window.location = 'http://large-screen-url/';
});
。这会将问题变成一个常规的“钻石继承”问题,但我似乎仍然无法让编译器做我想做的事。
将另一个类的实现用于父类的虚方法有什么方法?
答案 0 :(得分:0)
我相信你想做这样的事情。
// The interface.
class IInterface
{
public:
virtual ~IInterface() {}
virtual void method1() = 0;
virtual void method2() = 0;
};
// A partial, abstract implementation of interface.
// This base class implements method1 but leaves method2 unimplemented,
// which means this class cannot be instantiated.
class PartialImplementation : public IInterface
{
public:
virtual void method1() {}
};
// Full implementation that uses the partial implementation above.
// method1's implementation is from the base class.
// method2's implementation is in this class.
// Because all methods are implemented (directly or inherited), this class can be instantiated.
class FullImplementation1 : public PartialImplementation
{
public:
void method2() {}
};
// Full implementation that uses the partial implementation above.
// method1's implementation is in this class (overrides base implementation).
// method2's implementation is in this class.
// Because all methods are implemented (directly), this class can be instantiated.
class FullImplementation2 : public PartialImplementation
{
public:
void method1() {}
void method2() {}
};
// Full implementation that DOES NOT use the partial implementation above.
// method1's implementation is in this class.
// method2's implementation is in this class.
// Because all methods are implemented (directly), this class can be instantiated.
class FullImplementation3 : public IInterface
{
public:
void method1() {}
void method2() {}
};
int main()
{
FullImplementation1 i1; // OK
FullImplementation2 i2; // OK
FullImplementation3 i3; // OK
IInterface* p1 = new FullImplementation1(); // OK
delete p1; // OK
IInterface* p2 = new FullImplementation2(); // OK
delete p2; // OK
IInterface* p3 = new FullImplementation3(); // OK
delete p3; // OK
return 0;
}
答案 1 :(得分:0)
我所看到的钻石继承的每个例子都来自糟糕的设计,因为对IS-A和HAS-A关系的误解。不要继承使用实现,继承提供实现(例如继承实现)。
SystemA
应该从System
继承,并且SomethingImplementation
作为其所调用方法的成员。
您也可以查看mixins。