新手提问:《C++ primer》中的一段代码看不懂
xjffj 2005-04-15 07:47:41 现在在看C++ primer,比国内的一些C++的书要详细多了,推荐看。
在第二章的练习2.15中给出了一个泛型类的定义如下,其中
explicit Example2(elemType val = 0):_val(val){}
中的冒号后的代码_val(val)是何意?为何能编译通过,很不明白。_val明明是一个变量,怎么能作为方法使用,难道是用于给_val初始化的?请高手指教,谢谢。
-----------------------------
#include <iostream>
using namespace std;
template <class elemType>
class Example2{
public:
explicit Example2(elemType val = 0):_val(val){}
bool min(elemType value) {return _value < value;}
void value(elemType new_val) {_val = new_val;}
void print(ostream& os) {os << _val;}
private:
elemType _val;
};
template<class elemType>
ostream& operator<<(ostream& os, const Example2<elemType>& ex){
ex.print(os);
return os;
}
int main(){
return 0;
}