该指针引用类的当前对象,并将其作为参数传递给另一个方法。在C++中, this指针 作为隐藏参数传递给所有非静态成员函数调用。
null
“this”指针的类型: 其类型取决于函数声明。此指针的类型为 常量示例类* 或 示例类*。 这取决于它是否位于 康斯特 或者 非常量 课堂教学方法 示例类 .
1) 如果声明了类X的成员函数 康斯特 ,这种类型是 常数X* 如下图所示,
CPP
// CPP Program to demonstrate // if the member function of a // class X is declared const #include <iostream> using namespace std; class X { void fun() const { // this is passed as hidden argument to fun(). // Type of this is const X* const } }; |
2) 如果声明了成员函数 不稳定的 这种类型是 挥发性X* 如下图所示,
CPP
// CPP Program to demonstrate // if the member function is // declared volatile #include <iostream> using namespace std; class X { void fun() volatile { // this is passed as hidden argument to fun(). // Type of this is volatile X* const } }; |
3) 如果声明了成员函数 常量挥发性 ,这种类型是 常数X* 如下图所示,
CPP
// CPP program to demonstrate // if the member function is // declared const volatile #include <iostream> using namespace std; class X { void fun() const volatile { // this is passed as hidden argument to fun(). // Type of this is const volatile X* const } }; |
请注意 康斯特 , 不稳定的 和 常量挥发性 是类型限定符。
注意:“这个” 指针不是一个 左值。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END