2012-06-19 2 views
0

у меня есть шаблонный класс, названный Cell, здесь определение:Вызов произнесения оператора шаблона базового класса в производном классе

template <class T> 
class OneCell 
{ 
..... 
} 

У меня есть оператор, отлитые из Cell Т, здесь

virtual operator const T() const 
{ 
    ..... 
} 

Теперь я производный класс, называемый DCell, здесь

template <class T> 
class DCell : public Cell<T> 
{ 
..... 
} 

Мне нужно переопределить оператор-оператор Cell (вставить немного, если), но после того, как мне нужно вызвать оператор-оператор Cell. В других методах это должно быть что-то вроде

virtual operator const T() const 
{ 
    if (...) 
    { 
     return Cell<T>::operator const T; 
    } 
    else throw ... 
} 

, но я получил ошибку компилятора

error: argument of type 'const int (Cell::)()const' does not match 'const int'

Что я могу сделать?

Благодарим вас и извините за мой бедный английский.

+0

Я вас вложил весь код, было бы лучше, –

ответ

2

Вы на самом деле не называя оператора:

return Cell<T>::operator const T(); 

Полный код:

template <class T> 
class OneCell 
{ 
public: 
    virtual operator const T() const 
{ 
     return T(); 
    } 
}; 

template <class T> 
class DCell : public OneCell<T> 
{ 
public: 
    virtual operator const T() const 
    { 
     cout << "operator called"; 
     return OneCell<T>::operator const T(); 
    } 
}; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    DCell<int> x; 
    int y = (int)x; 
} 
0

Не делайте оператор виртуальным. Вместо этого передайте вспомогательную функцию protected virtual.

template <class T> 
class Cell 
{ 
    public: 
     operator const T() const { return cvt_T(); } 
    protected: 
     virtual const T cvt_T() const; 
}; 

template <class T> 
class DCell : public Cell<T> 
{ 
    const T cvt_T() const 
    { 
     if (...) 
     { 
      return Cell<T>::cvt_T(); 
     } 
     else throw ... 
    } 
}; 

Это и другие хорошие практики можно извлечь из GotW, here is the section on virtual architecture.

+0

Что такое виртуальный в коде? –

+0

@KerrekSB Я предполагаю, что 'cvt_T' должно быть. –

+0

@ Kerrek, Luchian: Спасибо, что указали это. Лучиан был прав. –

3

Вы отсутствуют круглые скобки, поэтому компилятор думал, что вы пытаетесь вернуть функцию-член, не называй Это.

 return Cell<T>::operator const T(); 
1

Рассмотрим этот код с реализациями Cell и DCell:

#include <iostream> 
#include <exception> 

template<class T> 
class Cell 
{ 
protected: 
    T cnt; 
public: 
    Cell(const T& cnt = T()) : cnt(cnt){} 
    virtual operator const T() const { return cnt; } 
}; 

bool test_bool = true; 

template<class T> 
class DCell : public Cell<T> 
{ 
public: 
    DCell(const T& cnt = T()) : Cell<T>(cnt){} 
    virtual operator const T() const 
    { 
     if(test_bool) 
     { 
      return Cell<T>::operator const T(); // Here you had Cell<T>::operator const T; 
     } else { 
      throw std::exception(); 
     } 
    } 
}; 

int main() 
{ 
    DCell<int> cell(5); 
    std::cout << static_cast<int>(cell) << "\n"; // prints 5 (and a new line) 
    return 0; 
} 
Смежные вопросы