何时在C ++的上下文中使用`this`

时间:2017-09-14 23:20:33

标签: c++ this

我试图修改面向对象的编程概念。在浏览这个基本的C ++示例here时,我发现在设置成员变量的值时没有使用this->关键字。然后,我修改了此程序以设置使用this关键字。令人惊讶的是,两者都有效(thisthis)。

#include <iostream>      // for cout and cin

class Cat                   // begin declaration of the class
{
  public:                    // begin public section
    Cat(int initialAge);     // constructor
    Cat(const Cat& copy_from); //copy constructor
    Cat& operator=(const Cat& copy_from); //copy assignment
    ~Cat();                  // destructor

    int GetAge() const;            // accessor function
    void SetAge(int age);    // accessor function
    void Meow();
 private:                   // begin private section
    int itsAge;              // member variable
    char * string;
};

 // constructor of Cat,
Cat::Cat(int initialAge)
{
  itsAge = initialAge;
  string = new char[10]();
}

//copy constructor for making a new copy of a Cat
Cat::Cat(const Cat& copy_from) {
   itsAge = copy_from.itsAge;
   string = new char[10]();
   std::copy(copy_from.string+0, copy_from.string+10, string);
}

//copy assignment for assigning a value from one Cat to another
Cat& Cat::operator=(const Cat& copy_from) {
   itsAge = copy_from.itsAge;
   std::copy(copy_from.string+0, copy_from.string+10, string);
}

Cat::~Cat()                 // destructor, just an example
{
    delete[] string;
}

// GetAge, Public accessor function
// returns value of itsAge member
int Cat::GetAge() const
{
   return itsAge;
}

// Definition of SetAge, public
// accessor function

 void Cat::SetAge(int age)
{
   // set member variable its age to
   // value passed in by parameter age
   itsAge = age;
}

// definition of Meow method
// returns: void
// parameters: None
// action: Prints "meow" to screen
void Cat::Meow()
{
   cout << "Meow.\n";
}

// create a cat, set its age, have it
// meow, tell us its age, then meow again.
int main()
{
  int Age;
  cout<<"How old is Frisky? ";
  cin>>Age;
  Cat Frisky(Age);
  Frisky.Meow();
  cout << "Frisky is a cat who is " ;
  cout << Frisky.GetAge() << " years old.\n";
  Frisky.Meow();
  Age++;
  Frisky.SetAge(Age);
  cout << "Now Frisky is " ;
  cout << Frisky.GetAge() << " years old.\n";
  return 0;
}

所以我的问题是,我们是否应该在此上下文中使用this关键字(在设置成员变量的值时)?谢谢!

修改或者,这是个人偏好,如上所述here

2 个答案:

答案 0 :(得分:0)

在示例的上下文中,关键字this通常用于自我文档,以区分类的非静态成员与局部变量。

答案 1 :(得分:0)

构造

// constructor of Cat,
Cat::Cat(int initialAge)
{
  itsAge = initialAge;
  string = new char[10]();
}

你应该把它写成:

// constructor of Cat,
Cat::Cat(int itsAge)
: itsAge(itsAge),
string(new char[10])
{
}

复制构造函数:

//copy constructor for making a new copy of a Cat
Cat::Cat(const Cat& copy_from) {
   itsAge = copy_from.itsAge;
   string = new char[10]();
   std::copy(copy_from.string+0, copy_from.string+10, string);
}

你应该把它写成:

Cat::Cat(const Cat& copy_from)
: itsAge(copy_from.itsAge),
string(new char[10])
{
   std::copy(copy_from.string, copy_from.string+sizeof string, string);
}

复制作业:

//copy assignment for assigning a value from one Cat to another
Cat& Cat::operator=(const Cat& copy_from) {
   itsAge = copy_from.itsAge;
   std::copy(copy_from.string+0, copy_from.string+10, string);
}

你应该把它写成:

Cat& Cat::operator=(const Cat& copy_from)
{
   this->itsAge = copy_from.itsAge;
   std::copy(copy_from.string, copy_from.string + sizeof string, string);
}

的Mutator:

// Definition of SetAge, public
// accessor function

void Cat::SetAge(int age)
{
   // set member variable its age to
   // value passed in by parameter age
   itsAge = age;
}

你应该把它写成:

void Cat::SetAge(int itsAge)
{
   this->itsAge = itsAgege;
}

换句话说:

  • 尽可能使用成员初始化列表
  • 使用相同的名称作为他们进入的成员的参数,以节省标识符并提高清晰度
  • 在必要时使用this->来消除歧义或错误。