英文:
How to implement derived classes for interfaces?
问题
我已经检查了一些帖子,但似乎没有涵盖我的问题。
我有一个充满了纯虚拟函数的接口。
我有一个接口的实现类,该类由各种公共基类组成。
尽管实现类中的方法可用,但错误显示虚拟函数未被实现。
将接口从实现中移除,一切都完美。
为什么实现的方法不能通过接口链接?
以下是代码...
class FunctionalClass1 {
public:
int result1(int _r) {
return 1;
}
int result2(int _r) {
return 2;
}
};
class FunctionalClass2 {
public:
int result3(int _r) {
return 3;
}
int result4(int _r) {
return 4;
}
};
class MyInterface {
public:
virtual int result1(int _r) = 0;
virtual int result2(int _r) = 0;
virtual int result3(int _r) = 0;
virtual int result4(int _r) = 0;
};
class ImplementationClass1 : public FunctionalClass1, public FunctionalClass2 {
};
class ImplementationClass2 : public FunctionalClass1, public FunctionalClass2, public MyInterface {
};
ImplementationClass1* ic1 = new ImplementationClass1(); // 完美,具有所有可用方法
ImplementationClass2* ic2 = new ImplementationClass2(); // 失败,因为接口未实现
我不理解为什么实现类的公共方法不能链接到接口。如果有人能解释为什么会失败,那就太好了。如果有人能提供解决方案,那就太棒了。
非常感谢
英文:
I have examined a few posts but nothing seemed to cover my question.
I have an interface full of pure virtuals.
I have an implementation class of the interface which is made up from various public base classes.
Even though the methods are available at the implementation class the error says the virtuals are not implemented.
Remove the interface from the implementation and it is perfect.
Why aren't the implementation methods linked through to the interface?
Here is the code...
class FunctionalClass1 {
public:
int result1(int _r) {
return 1;
}
int result2(int _r) {
return 2;
}
};
class FunctionalClass2 {
public:
int result3(int _r) {
return 3;
}
int result4(int _r) {
return 4;
}
};
class MyInterface {
public:
virtual int result1(int _r) = 0;
virtual int result2(int _r) = 0;
virtual int result3(int _r) = 0;
virtual int result4(int _r) = 0;
};
class ImplementationClass1 : public FunctionalClass1, public FunctionalClass2 {
};
class ImplementationClass2 : public FunctionalClass1, public FunctionalClass2, public MyInterface {
};
ImplementationClass1* ic1 = new ImplementationClass1(); // perfect, has all the methods available
ImplementationClass2* ic2 = new ImplementationClass2(); // fails because interface not implemented
1>X:\VisualStudioRepository\...\Main.cpp(37,57): error C2259: 'ImplementationClass2': cannot instantiate abstract class
1>X:\VisualStudioRepository\...\mainclass.h(33,7): message : see declaration of 'ImplementationClass2'
1>X:\VisualStudioRepository\...\Main.cpp(37,57): message : due to following members:
1>X:\VisualStudioRepository\...\Main.cpp(37,57): message : 'int MyInterface::result1(int)': is abstract
1>X:\VisualStudioRepository\...\mainclass.h(24,14): message : see declaration of 'MyInterface::result1'
1>X:\VisualStudioRepository\...\Main.cpp(37,57): message : 'int MyInterface::result2(int)': is abstract
1>X:\VisualStudioRepository\...\mainclass.h(25,14): message : see declaration of 'MyInterface::result2'
1>X:\VisualStudioRepository\...\Main.cpp(37,57): message : 'int MyInterface::result3(int)': is abstract
1>X:\VisualStudioRepository\...\mainclass.h(26,14): message : see declaration of 'MyInterface::result3'
1>X:\VisualStudioRepository\...\Main.cpp(37,57): message : 'int MyInterface::result4(int)': is abstract
1>X:\VisualStudioRepository\...\mainclass.h(27,14): message : see declaration of 'MyInterface::result4'
I do not understand by public methods of the implementation class are not linked through to the interface. It would be nice if someone could explain why it fails. It would be great if someone could provide a solution.
Many Thanks
答案1
得分: 2
以下是翻译好的代码部分:
要实现你想要的效果,一种方法是让 FunctionalClass1
和 FunctionalClass2
实现 MyInterface
接口:
class FunctionalClass1 : public virtual MyInterface {
public:
int result1(int _r) override {
return 1;
}
int result2(int _r) override {
return 2;
}
};
class FunctionalClass2 : public virtual MyInterface{
public:
int result3(int _r) override {
return 3;
}
int result4(int _r) override {
return 4;
}
};
class ImplementationClass2 : public FunctionalClass1, public FunctionalClass2 {
};
请注意,这些类现在将成为抽象类,因为它们具有未实现的虚拟方法。此外,MyInterface
需要虚拟继承,以便 ImplementationClass2
只有一个 MyInterface
父类。
另一种选项是在 ImplementationClass2
中提供重写,将其委托给 FunctionalClass1
和 FunctionalClass2
:
class ImplementationClass2 : public FunctionalClass1, public FunctionalClass2, public MyInterface {
public:
int result1(int _r) override {
return FunctionalClass1::result1(_r);
}
int result2(int _r) override {
return FunctionalClass1::result2(_r);
}
int result3(int _r) override {
return FunctionalClass2::result3(_r);
}
int result4(int _r) override {
return FunctionalClass2::result4(_r);
}
};
英文:
One way to achieve what you want would be to have FunciontalClass1
and FunctionalClass2
implement MyInterface
:
class FunctionalClass1 : public virtual MyInterface {
public:
int result1(int _r) override {
return 1;
}
int result2(int _r) override {
return 2;
}
};
class FunctionalClass2 : public virtual MyInterface{
public:
int result3(int _r) override {
return 3;
}
int result4(int _r) override {
return 4;
}
};
class ImplementationClass2 : public FunctionalClass1, public FunctionalClass2 {
};
Note that these classes will now be abstract because they have un-implemented virtual methods. Also MyInterface
needs to be virtually inherited so that ImplementationClass2
only gets a single MyInterface
parent.
Another option is to provide overrides in ImplementationClass2
that delegate to FunctionalClass1
and FunctionalClass2
:
class ImplementationClass2 : public FunctionalClass1, public FunctionalClass2, public MyInterface {
public:
int result1(int _r) override {
return FunctionalClass1::result1(_r);
}
int result2(int _r) override {
return FunctionalClass1::result2(_r);
}
int result3(int _r) override {
return FunctionalClass2::result3(_r);
}
int result4(int _r) override {
return FunctionalClass2::result4(_r);
}
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论