У меня есть класс, как показано ниже:экземпляра вложенного шаблона класса
template <class T>
class outer {
public:
typedef T TType;
std::vector <TType> v;
int f1 (TType t) {cout << "t= " << t << endl;};
class inner {
public:
inner& operator=(const inner &in) {return *this;}
void f2(const inner &in) {cout << "in f2\n";}
};
inner read() {cout << "in read\n";return inner();}
};
Outer должен иметь вложенные inner
. Я должен создать базовый класс для outer
(мы идем назад здесь!). Я должен был бы получить outer1
с базы. Существующие клиенты outer
должны работать, не меняя ничего. outer
должен просто добавить код для получения базового класса.
Мое решение это:
template <typename T>
class outer_iface {
public:
typedef T TType;
std::vector <TType> v;
virtual int f1(TType t) {cout << "bt= " << t << endl;};
template <typename U>
class inner_iface {
public:
using value_type = U;
inner_iface& operator=(const inner_iface &in)
{
return static_cast <U*> (this)->operator=(in);
}
void f2(const inner_iface &in)
{
return static_cast <U*> (this)->f2(in);
}
}; //inner_biface
/*template <typename U>
typename inner_iface <U>::value_type read()
{
return static_cast <U*> (this)->read();
}*/
};
template <typename T>
class outer : public outer_iface <T> {
public:
typedef T TType;
std::vector <TType> v;
int f1 (TType t) {cout << "t= " << t << endl;};
class inner : public outer_iface <T> :: template inner_iface <inner> {
public:
inner& operator=(const inner &in) {return *this;}
void f2(const inner &in) {cout << "in f2\n";}
};
inner read() {cout << "in read\n";return inner();}
};
Это компилирует и строит. Но, у меня есть 2 вопроса:
- мое объявление/определение
read
правильной вouter_iface
? - Как я могу создать экземпляр
outer_iface
, с указаниемint
и позвонить по телефонуread
? Я попытался с
main()
:outer_iface<int> oi; oi.read();
лязг дал ошибки:
g++ -g --std=c++11 test7.cpp
test7.cpp: In function ‘int main()’:
test7.cpp:62:11: error: no matching function for call to
‘outer_iface<int>::read()’oi.read();
^
test7.cpp:62:11: note: candidate is:
test7.cpp:28:40: note: template<class U> typename
outer_iface<T>::inner_iface<U>::value_type outer_iface<T>::read()
[with U = U; T = int]
typename inner_iface <U>::value_type read()
^
test7.cpp:28:40: note: template argument deduction/substitution failed:
test7.cpp:62:11: note: couldn't deduce template parameter ‘U’
oi.read();
Таким образом, очевидно, что я не правильно. Как я могу исправить inner_face::read
? Любая помощь/проницательность оценена. благодаря SDP
Что такое "outer1"? * Пожалуйста, не используйте чрезвычайно запутывающие имена, такие как «verylongname» и «very1ongname» и «veryIongname1». Можем ли мы просто иметь «Foo» и «Bar»? –
Зачем вводить CRTP для 'inner'? – Jarod42