Reputation: 69
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 :)
Upvotes: 0
Views: 72
Reputation: 93
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);
}
};
Upvotes: 2