嵌套数据成员指针 - 不可能吗?
下面的简化代码示例除了对数据成员指针进行两次后续赋值之外,没有做任何有用的事情。第一个分配有效,第二个分配给出编译器错误。大概是因为它是嵌套成员。
问题是:是否真的不可能让成员指针指向嵌套成员,或者我是否缺少任何奇特的语法?
struct Color {
float Red;
float Green;
float Blue; };
struct Material {
float Brightness;
Color DiffuseColor; };
int main() {
float Material::* ParamToAnimate;
ParamToAnimate = &Material::Brightness; // Ok
ParamToAnimate = &Material::DiffuseColor.Red; // Error! *whimper*
return 0; }
ATM 我正在通过使用字节偏移和大量强制转换来解决问题。但这很丑陋,我最好使用那些成员指针。
是的,我知道这个问题以前肯定出现过(就像几乎任何问题一样)。是的,我事先搜索过但没有找到令人满意的答案。
感谢您抽出时间。
The following reduced code sample does not do anything useful but two subsequent assignments to a data member pointer. The first assignment works, the second one gives a compiler error. Presumably because its to a nested member.
Question would be: Is it really just not possible to let a member pointer point to a nested member or am I missing any fancy syntax there?
struct Color {
float Red;
float Green;
float Blue; };
struct Material {
float Brightness;
Color DiffuseColor; };
int main() {
float Material::* ParamToAnimate;
ParamToAnimate = &Material::Brightness; // Ok
ParamToAnimate = &Material::DiffuseColor.Red; // Error! *whimper*
return 0; }
ATM I am working around by using byte offsets and a lot of casts. But that is ugly, I would better like to use those member pointers.
Yes, I know that question surely arised before (like nearly any question). Yes, I searched beforehand but found no satisfying answer.
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
AFAIK,这是不可能的。指向成员的指针只能由
&qualified_id
类型的表达式形成,这不是您的情况。Vite Falcon的解决方案可能是最合适的。
AFAIK, this is not possible. A pointer-to-member can only be formed by an expression of type
&qualified_id
, which is not your case.Vite Falcon's solution is probably the most appropriate.
我假设您正在尝试获取指向数据成员
Red
的指针。由于这是在结构Color
中定义的,因此指针的类型为Color::*
。因此,您的代码应该是:要使用它,您需要将其绑定到
Color
的实例,例如:编辑:是否无法使动画函数成为模板?例如:
I assume you are trying to get the pointer to the datamember
Red
. Since this is defined in the structColor
the type of the pointer isColor::*
. Hence your code should be:To use it, you need to bind it to an instance of
Color
for example:EDIT: Is it not possible to make the animation function a template? For example:
您可以使用函子来代替成员指针,当给定
Material
实例时,该函子返回一个float*
;将ParamToAnimate
的类型更改为:std::function
从好的方面来说,它是可移植的 - 但从坏的方面来说,它需要大量的样板代码并且具有大量的运行时开销。
如果这对性能至关重要,我会倾向于坚持使用偏移方法。
Instead of a member pointer, you can use a functor that returns a
float*
when given an instance ofMaterial
; change the type ofParamToAnimate
to something like:std::function<float*(Material&)>
On the plus side, it's portable - but on the downside, it requires a significant amount of boilerplate code and has significant runtime overhead.
If this is performance critical, I'd be tempted to stick with the offset method.
基本上,您正在尝试获取指向可以设置动画的浮点变量的指针。为什么不使用
float*
。您遇到的问题是Brightness
是 Material 的成员,但是Red
是Color
的成员,而不是Material
,交给编译器。使用float*
应该可以解决您的问题。Basically you're trying to get a pointer to a float variable that you can animate. Why not use
float*
. The issue you're having there is thatBrightness
is a member of Material, however,Red
is a member ofColor
and notMaterial
, to the compiler. Usingfloat*
should solve your problem.这是不可能的。但有一个解决方法非常接近您想要实现的目标。它涉及将嵌套成员与“布局兼容”的匿名结构一起放入联合中。缺点是接口有点臃肿,并且需要保持同级结构的定义同步。
ISO IEC 14882-2003 (c++03):
多重嵌套也是可能的:
It's not possible. But there is a workaround very close to what you want to achieve. It involves putting the nested member into an union alongside with a "layout-compatible" anonymous struct. The downside is a bit bloated interface and the need of keeping definitions of sibling structs in sync.
ISO IEC 14882-2003 (c++03):
Multiple nesting is possible too:
用继承代替组合怎么样?
How about inheritance instead of composition?
您可以简单地进行重构,这样就根本没有嵌套结构。添加一个设置器,而不是将颜色解压到其组成部分中,以便现有代码不需要进行太多更改,然后从那里开始。
您还可以采用可选的第二个指针来挖掘嵌套类型。与当前方法相比,检查是否需要第二个参数的单个测试可能足够好,并且如果稍后出现其他字段,则更容易扩展。
更进一步,您将拥有一个带有虚拟
Dereference
方法的基MaterialPointer
类。案例类可以处理简单成员,派生类可以处理嵌套成员以及查找它们所需的任何附加信息。然后工厂可以生产适当类型的MaterialMember*
对象。当然,现在您陷入了堆分配的困境,因此这可能有点太不切实际了。You could simply refactor such that you don't have the nested structure at all. Add a setter than unpacks the color into its component parts so that existing code need not change much, and go from there.
You could also take an optional second pointer that digs into the nested type. A single test to see if you need the second parameter may prove good enough compared to your current method, and would be more easily extended should additional fields turn up later.
Take that a step further, and you have a base
MaterialPointer
class with a virtualDereference
method. The case class can handle simple members, with derived classes handling nested members with whatever additional information they need to find them. A factory can then produceMaterialMember*
objects of the appropriate type. Of course, now you're stuck with heap allocations, so this is likely a little too far to be practical.由于在某些时候您需要一个指向实际数据的指针,因此这可能适合您,也可能不适合您:
Since at some point you need a pointer to the actual data, this may or may not work for you: