【问题标题】:please explain this-> in c++ [duplicate]请用c ++解释这个-> [重复]
【发布时间】:2017-08-10 06:25:56
【问题描述】:

所以我看到了这个语法

这个->

在 udemy 的 c++ 教程中使用过,但没有向我解释为什么我会在编程中使用它。任何人都可以提供一个示例程序或正确的示例代码来显示它的作用吗?请解释其背后的逻辑。请不要说谷歌。我知道它与指针和内存有关。而我想我正在寻找的另一件事是一个五岁的孩子会理解或分解到足够容易理解的解释。

【问题讨论】:

  • 谷歌它。 Stackoverflow 已经有很多 QA 可以回答您的问题。
  • 在我看来这个教程不是很好。拿起good book instead/as well

标签: c++ pointers memory this


【解决方案1】:

this 关键字用于引用给定类的当前实例,例如:

class A {
   public:

       void setName(std::string name) {
           // if you would use name variable directly it 
           // will refer to the function parameter, 
           //hence to refer the field of the class you need to use this
           this->name = name;
       }
   private:
       std::string namel
}

【讨论】:

    猜你喜欢
    • 2015-03-28
    • 2013-03-19
    • 2013-12-25
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 2012-02-21
    • 1970-01-01
    相关资源
    最近更新 更多