Derived<T>
继承自Base<T>
。Derived<T>::m
应该返回一个Derived<T>::Inner
对象。什么错误,我不明白。
考虑以下代码:
template <typename T>
class Base {
public:
class Inner {
public:
virtual void x() = 0;
};
virtual Inner m(std::string arg) = 0;
};
template <typename U>
class Derived : Base<U> {
public:
class Inner : Base<U>::Inner {
public:
virtual void x();
};
virtual Inner m(std::string arg);
};
template <typename U>
Derived<U>::Inner Derived<U>::m(std::string arg) {
std::cout << "calling Derived::m() " << arg << std::endl;
return new Inner();
}
template <typename U>
void Derived<U>::Inner::x() {
std::cout << "calling x" << std::endl;
}
Derived<std::string>* d = new Derived<std::string>();
Derived<std::string>::Inner* inner = d->m("test");
inner->x();
我收到以下错误:
invalid covariant return type for
'Derived<U>::Inner Derived<U>::m(std::string) [with U = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]'
overriding 'Base<T>::Inner Base<T>::m(std::string) [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]'