在C 中,在此上下文中使用``''

When to use `this` in this context in C++

本文关键字:上下文      更新时间:2023-10-16

我试图修改面向对象的编程概念。在此处浏览此基本C 示例时,我发现在为成员变量设置值时尚未使用this->关键字。然后,我修改了此程序以使用this关键字。令人惊讶的是,这两个工作(这个和这个)。

#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关键字?谢谢!

编辑:或,此处提到的这是A 个人喜好

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

构造函数:

// 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);
}

突变器:

// 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->消除歧义或错误。