template <class T>
Vector<T>::Vector() : _size_(0){
this->_capacity_ = 10;
buffer = new T[this->_capacity_];
}
template <class T>
Vector<T>::Vector(unsigned int s) {
this->_size_ = s;
this->_capacity_ = _size_;
this->buffer = new T[this->_capacity_];
init(0, this->_size_);
}
template <class T>
Vector<T>::Vector(unsigned int s, const T &initial){
this->_size_ = s;
this->_capacity_ = s;
this->buffer = new T[this->_capacity_];
init(0, s, initial);
}
我的代碼使用了很多關鍵字this
。如果沒有this
關鍵字,那麼在類中調用成員函數而不是直接訪問它是否被認爲是好的做法?如果我總是調用成員函數來訪問成員變量,是否會產生開銷? C++實現是做什麼的?我應該使用「this」關鍵字從成員函數內部訪問類成員嗎?
是你的問題是否使用'this'來訪問成員/方法,或者是否使用getters和setter而不是直接訪問成員?我不清楚。 – David
你不應該使用'this'。它使代碼變得龐大並且使其不易讀。如果你想輸入你的'this',爲什麼要用C++?它看起來「奇怪」(即沒有人寫這樣的代碼,這很瘋狂)。 –