C++ 中的 ->* 和 .* 操作符是做什么用的
rt;
在看操作符重载时看到这两个符号。发现这两个操作符 根本没用过,书上也没说怎么用的。忘前辈们指点
主要: ->*和.*是连在一起的。
google了一下 有找到一些例子可是 运行读是错的
一个简单的例子如下
class ob
{
public:
int *pInt;
};
//有人说这么调用
ob test;
test->*pInt;
//这样我在VS2008上面根本运行不了
错误提示:1>d:\personal\my documents\visual studio 2008\projects\test2\test2\test2.cpp(100) : error C2065: 'pInt' : undeclared identifier
C++
[解决办法]
C++新加的运算符:成员指针。
http://en.cppreference.com/w/cpp/language/operator_precedence
[解决办法]
一个是堆上对象使用,一个是栈上对象使用。
[解决办法]
struct dummy
{
int a;
};
int main()
{
dummy d = {10};
int dummy::*p = &dummy::a;
d.*p = -1;
return d.*p;
}
class trainRoom{
public:
int seat57, seat58, seat59;
double washroom;
};
int main()
{ int trainRoom::*p; //这个指针没有初始化,变量p里放的是垃圾值,危险
p=&trainRoom::seat59;
trainRoom room3;
room3.*p=1; //相当于room3.seat59=1;
return 0;
}
//你的代码
class ob
{
public:
int *pInt;
};
obj test;
int a;
test.pInt = &a;
std::cout << *(test.pInt) << std::endl;