gpt4 book ai didi

c++ - 在对象中使用或不使用 'this'

转载 作者:行者123 更新时间:2023-11-28 02:45:52 26 4
gpt4 key购买 nike

<分区>

我的问题是指当我想调用同一个类的其他方法时的情况。使用和不使用“this”有什么区别?与类的变量相同。通过“this”访问这些变量有区别吗?是否与那些方法/变量是私有(private)的还是公共(public)的有关?示例:

class A {
private:
int i;
void print_i () { cout << i << endl; }

public:
void do_something () {

this->print_i(); //call with 'this', or ...
print_i(); //... call without 'this'

this->i = 5; //same with setting the member;
i = 5;
}
};

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com