【问题标题】:Why can't access static file with this pointer?为什么不能用这个指针访问静态文件?
【发布时间】:2018-09-16 13:41:46
【问题描述】:

例如我有一个程序:

class TestStatic
 {
   private:<br>
    static int staticvariable;

   public:<br>
    TestStatic() {
    this->staticvariable = 0;
    cout << this->staticvariable;
}

~TestStatic() {}
};

int main() {
 TestStatic object;
 return 0;
}

为什么这个指针不能访问 staticvariable 。我不明白为什么。

【问题讨论】:

    标签: pointers static this


    【解决方案1】:

    可能是因为 staticvariable 未绑定到 this 而是绑定到您的班级。 查看以下答案:

    希望对你有帮助。

    【讨论】:

    • 谢谢你的回答,但是,我也不知道为什么。该指针可以访问的文件类型是什么
    • 啊,好的,我刚刚明白了,问题不是这个指针
    • 不,问题不在于指针。您可以使用类的名称访问静态变量,后跟两个冒号 (:)。在你的情况下,这将是TestStatic::staticvariable
    猜你喜欢
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    相关资源
    最近更新 更多