代码之家  ›  专栏  ›  技术社区  ›  user8305079

在C中何时在这个上下文中使用this++

  •  0
  • user8305079  · 技术社区  · 7 年前

    我试图修改我的面向对象编程概念。在学习这个基本的C++示例时 here this-> this 关键字。令人惊讶的是,两者都有效( 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;
    }
    

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

    编辑: 个人偏好 here

    2 回复  |  直到 7 年前
        1
  •  0
  •   Vlad from Moscow    7 年前

    this 通常用于自文档,以区分类的非静态成员和局部变量。

        2
  •  0
  •   user207421    7 年前

    建造商:

    // 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->