我应该使用"this"关键字从内部成员函数访问类成员吗?

jax*_*jax 0 c++ oop function

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);
}
Run Code Online (Sandbox Code Playgroud)

我的代码经常使用this关键字.在没有this关键字的情况下,在类中调用成员函数而不是直接访问它是一种良好的做法吗?如果我总是调用成员函数来访问成员变量,它会产生开销吗?C++实现做了什么?

Rob*_*Rob 6

没有开销,因为代码是编译的.当你这样做时:

this->_size = 5;
Run Code Online (Sandbox Code Playgroud)

_size=5
Run Code Online (Sandbox Code Playgroud)

编译器将它们视为相同并生成相同的代码.

如果您喜欢使用'this',请使用它.

我个人不喜欢它.