如果我可以使用指向类中变量的普通指针,那么类成员指针的意义何在?
拿这段代码来说:
struct mystruct
{
int var;
mystruct() : var(0) {}
};
int main()
{
mystruct ins;
int* p = &ins.var;
*p = 1;
}
那么,类成员指针的一些具体的、非常好的使用示例有哪些呢?
int X::*p = &X::data; /* p contains offset */
X object;
X *objptr = new X;
int i = object.*p;
int j = objptr->*p;
Take this code:
struct mystruct
{
int var;
mystruct() : var(0) {}
};
int main()
{
mystruct ins;
int* p = &ins.var;
*p = 1;
}
So what are some concrete really good examples of uses for the class member pointer?
int X::*p = &X::data; /* p contains offset */
X object;
X *objptr = new X;
int i = object.*p;
int j = objptr->*p;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来您的问题是关于指向数据成员类型的指针。在 C++ 中,还有指向成员函数的指针类型。两者在某些抽象层面上有一些共同点,但在其他方面却有所不同。
指向数据成员类型的指针适用于该类的任何实例。普通指针总是指向类的特定实例的成员。指向数据成员类型的指针是从类对象开头开始的“运行时偏移”思想的更高级别实现。它是一个相对指针。这样,它与普通指针完全不同,普通指针是绝对指针。
为了通过示例来说明这一点,假设您有一个类数组,其中包含三个相同类型的成员
x
、y
和z
并且您需要将整个数组中所有同名的成员设置为
0
,而不更改任何其他成员。下面是如何使用指向数据成员类型的指针来完成此操作。现在,通过使用此函数,您可以
将所有
x
设置为零。或者您可以将所有 y 设置为零。您可以使用一个单个函数来完成所有这些操作,而且同样重要的是,成员选择是由运行时参数执行的(而不是编译时参数)时间一)。
你不能用普通的指针做这样的事情。事实上,你无法通过任何其他方式做到这一点。
It seems the your question is about pointers of pointer-to-data-member type. In C++ there are also pointers of pointer-to-member-function type. The two have something in common at some abstract level, but otherwise they are different.
Pointer of pointer-to-data-member type is applicable to any instance of the class. An ordinary pointer always points to a member of a specific instance of the class. Pointer of pointer-to-data-member type is a higher level implementation of the idea of "run-time offset" from the beginning of a class object. It is a relative pointer. In that way it is completely different thing from ordinary pointers, which are absolute pointers.
To illustrate that with an example, let's say you have an array of classes with three members of the same type
x
,y
andz
and you need to set all members with the same name to
0
in the entire array, without changing any other members. Here's how you can do it using a pointer of pointer-to-data-member typeNow, by using this function you can do
to set all
x
s to zero. Or you can doto set all
y
s to zero. You do can all this with a single function, and what's also important, the member selection is performed by a run-time parameter (as opposed to a compile-time one).You cannot do something like this with ordinary pointers. In fact, you can't do it in any other way.
这样您就可以在多个实例上访问该成员。
So that you can access the member on multiple instances.
嗯,这类似于要求使用
goto
的良好示例...;-) 好的,它就在那里,有时它可能很方便,但是任何“良好”使用的示例goto
可能会引起争议。对于成员指针来说也是如此,它们的级别非常低。对于成员函数指针,一种常见的用法是在事件驱动系统中。将成员函数指针与指向相关类的对象的指针打包在一起,您就拥有了非常类似于 C# 委托的东西。然后,您可以传递这个小函子包,其他人的代码(例如 GUI 框架)可以回调您的对象,甚至不需要知道它。
Boost 库为此提供了一些支持,例如
boost::function
和boost::bind
,TR1 和即将推出的 C++0x 标准库(本质上是一个Boost 功能的子集)。不过,我无法立即想到数据成员指针的任何常见用法。
关于“低级别”,成员指针遵循特殊规则,这些规则可以让您无意中绕过
受保护
访问限制。干杯& hth.,
– 阿尔夫
Uhm, this is akin to asking for good examples of using
goto
... ;-) Okay, it's there, and sometimes it can be handy, but any example of "good" use ofgoto
is likely to be disputed. And so also for member pointers, they're pretty low-level.For member function pointers one common usage is in event-driven systems. Package a member function pointer together with a pointer to an object of the relevant class, and you have something very much like a C# delegate. You can then pass this little functor package around, and someone else's code, e.g. a GUI framework, can call back on your object without even knowing about it.
The Boost library provides some support for that, e.g.
boost::function
andboost::bind
, and so does TR1 and the upcoming C++0x standard library (essentially a subset of the Boost functionality).I can't offhand think of any common usage of data member pointers, though.
Regarding "low level", member pointers follow special rules which let you inadvertently bypass a
protected
access restriction.Cheers & hth.,
– Alf