嵌套数据成员指针 - 不可能? [英] Nested data member pointer - not possible?

查看:122
本文介绍了嵌套数据成员指针 - 不可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的缩减代码示例不会做任何有用的,但是两个后续的分配到数据成员指针。第一个assignement工作,第二个赋予编译器错误。大概是因为它是一个嵌套的成员。

The foolowing reduced code sample does not do anything usefull but two subsequent assignments to a data member pointer. The first assignement 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我正在使用字节偏移和大量的转换。但是这是丑陋的,我更喜欢使用那些成员指针。

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.

感谢您的时间。

推荐答案

AFAIK,这是不可能的。指向成员的指针只能由类型& qualified_id 的表达式形成,这不是您的情况。

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的解决方案可能是最合适的解决方案。

Vite Falcon's solution is probably the most appropriate.

这篇关于嵌套数据成员指针 - 不可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆