operator float() const { return f;}什么意思?(在线等)
<thinking c++> 15.3.3:
#include <assert.h>
#include <iostream.h>
template<class T,int size = 100>
class mblock{
T array[size];
public:
T& operator[](int index){
assert(index >= 0 && index < size);
return array[index];
}
};
class number{
float f;
public:
number (float F = 0.0f) : f(F) {}
number& operator=(const number& n) {
f = n.f;
return *this; //返回的是什么?(*this )是什么?
}
operator float() const { return f;}//这行看不懂!什么意思?
friend ostream&
operator<<(ostream& os, const number& x) {
return os << x.f;
}
};
template<class T, int sz = 20 >
class holder{
mblock<T, sz> *np;
public:
holder() : np(0) {}
number& operator[](int i){
assert( i >= 0 && i < sz) ;
if( !np ) np= new mblock<T,sz>;
return np->operator[](i);
}
};
main(){
holder<number,20> H;
for(int i = 0; i< 20; i++)
H[i] = i;
for(int j =0 ; j< 20 ;j++)
cout<< H[j] << endl;
}
以上两个问题解决就给分!谢!