My idea is to create a template out of interface such that it can return any container:
template <class T>
class IValue {
public:
virtual I& get() const = 0;
};
template<typename T>
class Value : public IValue<T>
{
public:
Value() :m_value()
{}
virtual T& get() const override
{
return m_value;
}
virtual ~Value()
{}
private:
T m_value;
};
class A
{
public:
A() {}
};
int main()
{
Value<A> a1;
//a1.get();
}
But I get compilation error as mentioned below:
$ c++ -std=c++14 try52.cpp
try52.cpp:4:17: error: 'I' does not name a type
virtual I& get() const = 0;
^
try52.cpp: In instantiation of 'class Value<A>':
try52.cpp:32:10: required from here
try52.cpp:14:16: error: 'T& Value<T>::get() const [with T = A]' marked override, but does not override
virtual T& get() const override
^
try52.cpp: In instantiation of 'T& Value<T>::get() const [with T = A]':
try52.cpp:34:1: required from here
try52.cpp:16:16: error: invalid initialization of reference of type 'A&' from expression of type 'const A'
return m_value;
How can I design or implement such a functionality?
Aucun commentaire:
Enregistrer un commentaire