用C++,有的概念不常用,有時候用的時候就忘記了。查閱大部頭的書比較麻煩。用Essential C++最好了。
手邊常備Essential C++。很不錯。
構造 析構:
析構函數無返回值,無參數。析構函數很多時候不用。用的情況一般是構造函數用 NEW 申請了堆空間。
若果有必要設計拷貝構造函數,那麼也一定有必要設計 copy assignment operator.一般有指針成員變量的話,需要設計拷貝構造函數。默認的拷貝構造不好用。
this 指針:
class demo
{
public:
int fundemo(int a,int b) {value1 = a,value2 = b}; // <=> int fundemo(int a,int b,demo * this){this.value1 = a,this.value2 = b};
private:
int value1;
int value2;
};
static:
static member function 類外定義的時候不需要 STATIC 關鍵字
static member data 類內相當於聲明,類外需要定義和初始化:int A::demostatic = 5;
class intBuffer {
public:
// ...
private:
//static const int _buf_size = 1024; // ok but not with VC++
enum { _buf_size = 1024 };
int _buffer[ _buf_size ]; // ok
};
運算符重載:
member operator this指針隱含代表左側操作數。
non -member operator 多以個參數。
嵌套型別: typedef 這裏就是這隻別名。typedef Triangular_iterator iterator;
友元: friend 聲明可以出現在類的任何位置,不受public 和private 的限制。使用是爲了效率。可以不用。
實現一個 copy assignment opterator:
matrix& matrix::operator=(const matrix& rhs)
{
if(*this != rhs)
{
_roh = rhs._roh;
delete [] _pmat;
_pmat = new double[elem_cnt];
}
return *this;
}
function object: -----------提供有function call 運算符的 class
class LessThan {
public:
LessThan( int val ) : _val( val ){}
int comp_val() const { return _val; }
void comp_val( int nval ){ _val = nval; }
bool operator()( int value ) const;
private:
int _val;
};
inline bool
LessThan::operator()( int value ) const
{ return value < _val; }
將 iostream運算符重載
<< 這個運算符必須是 non-member function。
>> 這個運算符需要考慮輸入問題,難設計。
指向member function 的指針
與指向 non-member function 的指針有些不同,必須要指出他是指向那個class 的。
這個比較陌生。 ->* .* 這裏會這麼用。