【发布时间】:2014-05-09 16:23:01
【问题描述】:
您好,我在继承和运算符重载方面遇到了一些问题,希望你们能给我一些说明。
我有以下课程:
template<typename Type>
class Predicate{
public:
Predicate() {};
virtual ~Predicate(){};
virtual bool operator()(const Type & value) = 0;
virtual bool operator()(const Type * value){ //<-- this is the operator thats not working
return (*this)(*value);
};
};
template<typename Type>
class Always : public Predicate<Type>{
public:
bool operator()(const Type & value){return true;}
~Always(){};
};
现在我希望我的所有谓词都接受引用和指针,但是当我测试类时:
int main(){
Always<int> a;
int i = 1000;
a(&i);
system("pause");
return 1;
}
我收到以下错误:
test.cpp: In function 'int main()':
test.cpp:10:6: error: invalid conversion from 'int*' to 'int' [-fpermissive]
a(&i);
^
In file included from test.cpp:2:0:
predicates.h:22:7: error: initializing argument 1 of 'bool Always<Type>::operator()(const Type&) [with Type = int]' [-fpermissive]
bool operator()(const Type & value){return true;}
【问题讨论】:
-
'现在我希望我的所有谓词都接受引用和指针' 可能这个
return (*this)(*value);应该使用适当的static_cast<>然后...
标签: c++ inheritance operator-overloading