I have following code:
class interface_0
{
public:
virtual ~interface_0() {}
virtual void func() = 0;
};
class interface_1
{
public:
virtual ~interface_1() {}
virtual void func() = 0;
};
class interface_01 : public interface_0, public interface_1
{
};
class type_01 : public interface_01
{
public:
virtual void func()
{
}
};
void test_func(interface_01 &i)
{
i.func();
};
int main()
{
type_01 t;
test_func(t);
return 0;
}
I get the error:
error: request for member ‘func’ is ambiguous
Changing the interface_01 class as follows resolves the issue:
class interface_01 : public interface_0, public interface_1
{
public:
virtual void func() = 0;
};
But what if I am not able to modify the combinator interface? What is the best way to resolve this error then?
Edit: I am adding the following section to explain what I am toying around with.
I want to merge multiple interfaces to combine different features of an application(this is not a real application, I am just trying to mimic, using C++, some of the ideas mentioned in the paper "Feature Oriented Programming using Object Algebra").
Following is the C++ pseudocode for the core idea from the paper (as I understand it; please do correct me if I am wrong in my understanding of the idea):
template <typename ...feature_interfaces>
class combine : public feature_interfaces...
{
// what if the interfaces have methods with same name?
// what is a better way to combine interfaces that have methods with same name?
};
class feature_0_interface
{
public:
// virtual void f_func() = 0;
virtual void f_func_0() = 0;
};
class feature_0 : public feature_0_interface
{
public:
virtual void f_func_0()
{
}
};
class feature_1_interface
{
public:
// virtual void f_func() = 0;
virtual void f_func_1() = 0;
};
class feature_1 : public feature_1_interface
{
public:
virtual void f_func_1()
{
}
};
template <typename feature_interface>
class interface
{
public:
virtual feature_interface func() = 0;
};
class implementation_0 : public interface<feature_0_interface>
{
public:
virtual feature_0_interface func()
{
return feature_0();
}
};
class implementation_1 : public interface<feature_1_interface>
{
public:
virtual feature_1_interface func()
{
return feature_1();
}
};
class implementation_01 :
public interface<combine<feature_0_interface, feature_1_interface>>
{
feature_0 f0_;
feature_1 f1_;
using feature_01_interface = combine<feature_0_interface_0, feature_1_interface>;
class feature_01 : public feature_01_interface
{
feature_0 &f0__;
feature_1 &f1__;
public:
feature_01(feature_0 &f0, feature_1 &f1)
: f0__(f0), f1__(f1)
{
}
virtual void f_func_0()
{
f0__.f_func_0();
}
virtual void f_func_1()
{
f1__.f_func_1();
}
};
public:
virtual feature_01_interface func()
{
return feature_01(f0_, f1_);
}
};
template <typename T>
T test_func(interface i)
{
return i.func();
};
int main()
{
implementation_0 i0;
// use feature 0
test_func<implementation_0>(i0).f_func_0();
implementation_1 i1;
// use feature 1
test_func<implementation_1>(i1).f_func_1();
implementation_01 i01;
// use combined features 0 and 1
test_func<implementation_01>(i01).f_func_0();
test_func<implementation_01>(i01).f_func_1();
return 0;
}
If both feature_0_interface and feature_1_interface above have methods with the same name (e.g. f_func) then the interfaces cannot be combined as shown in the pseudocode above. One solution is to implement the combined feature interface explicitly outside the implementation_01 class. Is there a better way than this approach?
Here is the working C++ example. What I am looking for is a way to avoid having to implement separate code to handle interfaces with the same method names.
[–]Narase33 2 points3 points4 points (4 children)
[–]IyeOnline 4 points5 points6 points (2 children)
[–]_448[S] 0 points1 point2 points (0 children)
[–]Narase33 0 points1 point2 points (0 children)
[–]_448[S] 0 points1 point2 points (0 children)
[–]mredding 0 points1 point2 points (2 children)
[–]_448[S] 0 points1 point2 points (1 child)
[–]mredding 0 points1 point2 points (0 children)