嵌套数据成员指针 - 不可能吗?

发布于 2024-09-13 16:02:04 字数 627 浏览 4 评论 0原文

下面的简化代码示例除了对数据成员指针进行两次后续赋值之外,没有做任何有用的事情。第一个分配有效,第二个分配给出编译器错误。大概是因为它是嵌套成员。

问题是:是否真的不可能让成员指针指向嵌套成员,或者我是否缺少任何奇特的语法?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

装纯掩盖桑 2024-09-20 16:02:04

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.

蓝礼 2024-09-20 16:02:04

我假设您正在尝试获取指向数据成员Red的指针。由于这是在结构 Color 中定义的,因此指针的类型为 Color::*。因此,您的代码应该是:

int main() {
    float Color::* ParamToAnimate;
    ParamToAnimate = &Color::Red; 
    return 0; }

要使用它,您需要将其绑定到 Color 的实例,例如:

void f(Color* p, float Color::* pParam)
{
    p->*pParam = 10.0;
}
int main() {
    float Color::* ParamToAnimate;
    ParamToAnimate = &Color::Red; 

    Material m;
    f(&m.DiffuseColor, ParamToAnimate);
    return 0;
}

编辑:是否无法使动画函数成为模板?例如:

template<class T>
void f(T* p, float T::* pParam)
{
    p->*pParam = 10.0;
}
int main() {

    Material m;

    f(&m.DiffuseColor, &Color::Red);
    f(&m, &Material::Brightness);
    return 0;
}

I assume you are trying to get the pointer to the datamember Red. Since this is defined in the struct Color the type of the pointer is Color::*. Hence your code should be:

int main() {
    float Color::* ParamToAnimate;
    ParamToAnimate = &Color::Red; 
    return 0; }

To use it, you need to bind it to an instance of Color for example:

void f(Color* p, float Color::* pParam)
{
    p->*pParam = 10.0;
}
int main() {
    float Color::* ParamToAnimate;
    ParamToAnimate = &Color::Red; 

    Material m;
    f(&m.DiffuseColor, ParamToAnimate);
    return 0;
}

EDIT: Is it not possible to make the animation function a template? For example:

template<class T>
void f(T* p, float T::* pParam)
{
    p->*pParam = 10.0;
}
int main() {

    Material m;

    f(&m.DiffuseColor, &Color::Red);
    f(&m, &Material::Brightness);
    return 0;
}
月牙弯弯 2024-09-20 16:02:04

您可以使用函子来代替成员指针,当给定 Material 实例时,该函子返回一个 float* ;将 ParamToAnimate 的类型更改为:

std::function

从好的方面来说,它是可移植的 - 但从坏的方面来说,它需要大量的样板代码并且具有大量的运行时开销。

如果这对性能至关重要,我会倾向于坚持使用偏移方法。

Instead of a member pointer, you can use a functor that returns a float* when given an instance of Material; change the type of ParamToAnimate 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.

小耗子 2024-09-20 16:02:04

基本上,您正在尝试获取指向可以设置动画的浮点变量的指针。为什么不使用float*。您遇到的问题是 Brightness 是 Material 的成员,但是 RedColor 的成员,而不是 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 that Brightness is a member of Material, however, Red is a member of Color and not Material, to the compiler. Using float* should solve your problem.

西瑶 2024-09-20 16:02:04

这是不可能的。但有一个解决方法非常接近您想要实现的目标。它涉及将嵌套成员与“布局兼容”的匿名结构一起放入联合中。缺点是接口有点臃肿,并且需要保持同级结构的定义同步。

struct Color {
    float Red;
    float Green;
    float Blue; };

struct Material {
    float Brightness;
    union {
        struct { // "Layout-compatible" with 'Color' (see citation below)
            float DiffuseColorRed;
            float DiffuseColorGreen;
            float DiffuseColorBlue; };
        Color DiffuseColor; }; };

int main() {
    Material M;

    float Material::* ParamToAnimate;
    ParamToAnimate = &Material::DiffuseColorRed;
    std::cin >> M.*ParamToAnimate;
    std::cout << M.DiffuseColor.Red << std::endl;
    return 0; }

ISO IEC 14882-2003 (c++03):

§3.9

11

如果两个类型 T1 和 T2 是同一类型,则 T1 和 T2 是
布局兼容的类型。 [注意:布局兼容的枚举是
7.2 中描述。布局兼容的 POD 结构和 POD 联合是
9.2 中描述。 ]

§9.2

16

如果 POD 联合包含两个或多个共享共同的 POD 结构
初始序列,如果 POD-union 对象当前包含一个
这些 POD 结构中,允许检查共同的初始
其中任何一个的一部分。两个 POD 结构共享一个共同的初始序列
如果相应的成员具有布局兼容的类型(并且,对于
位域,相同的宽度)用于一个或多个初始的序列
成员。

多重嵌套也是可能的:

struct Color {
    float Red;
    float Green;
    float Blue; };

struct Material {
    float Brightness;
    Color DiffuseColor; };

struct Wall {
    union {
        struct {
            float SurfaceBrightness;
            struct {
                float SurfaceDiffuseColorRed;
                float SurfaceDiffuseColorGreen;
                float SurfaceDiffuseColorBlue; }; };
        Material Surface; }; };

int main() {
    Wall W;

    float Wall::* ParamToAnimate;
    ParamToAnimate = &Wall::SurfaceDiffuseColorRed;
    std::cin >> W.*ParamToAnimate;
    std::cout << W.Surface.DiffuseColor.Red << std::endl;
    return 0; }

§9.2

14

两个 POD 结构(第 9 条)类型是布局兼容的,如果它们具有
相同数量的非静态数据成员,以及相应的非静态
数据成员(按顺序)具有布局兼容类型 (3.9)。

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.

struct Color {
    float Red;
    float Green;
    float Blue; };

struct Material {
    float Brightness;
    union {
        struct { // "Layout-compatible" with 'Color' (see citation below)
            float DiffuseColorRed;
            float DiffuseColorGreen;
            float DiffuseColorBlue; };
        Color DiffuseColor; }; };

int main() {
    Material M;

    float Material::* ParamToAnimate;
    ParamToAnimate = &Material::DiffuseColorRed;
    std::cin >> M.*ParamToAnimate;
    std::cout << M.DiffuseColor.Red << std::endl;
    return 0; }

ISO IEC 14882-2003 (c++03):

§3.9

11

If two types T1 and T2 are the same type, then T1 and T2 are
layout-compatible types. [Note: Layout-compatible enumerations are
described in 7.2. Layout-compatible POD-structs and POD-unions are
described in 9.2. ]

§9.2

16

If a POD-union contains two or more POD-structs that share a common
initial sequence, and if the POD-union object currently contains one
of these POD-structs, it is permitted to inspect the common initial
part of any of them. Two POD-structs share a common initial sequence
if corresponding members have layout-compatible types (and, for
bit-fields, the same widths) for a sequence of one or more initial
members.

Multiple nesting is possible too:

struct Color {
    float Red;
    float Green;
    float Blue; };

struct Material {
    float Brightness;
    Color DiffuseColor; };

struct Wall {
    union {
        struct {
            float SurfaceBrightness;
            struct {
                float SurfaceDiffuseColorRed;
                float SurfaceDiffuseColorGreen;
                float SurfaceDiffuseColorBlue; }; };
        Material Surface; }; };

int main() {
    Wall W;

    float Wall::* ParamToAnimate;
    ParamToAnimate = &Wall::SurfaceDiffuseColorRed;
    std::cin >> W.*ParamToAnimate;
    std::cout << W.Surface.DiffuseColor.Red << std::endl;
    return 0; }

§9.2

14

Two POD-struct (clause 9) types are layout-compatible if they have the
same number of nonstatic data members, and corresponding nonstatic
data members (in order) have layout-compatible types (3.9).

飞烟轻若梦 2024-09-20 16:02:04

用继承代替组合怎么样?

struct Color {
    float Red;
    float Green;
    float Blue; };

struct DiffuseColor : public Color {
    };

struct Material : public DiffuseColor {
    float Brightness; };


int main() {
    float Material::* ParamToAnimate;
    ParamToAnimate = &Material::Brightness;       // Ok
    ParamToAnimate = &Material::DiffuseColor::Red; // Ok! *whew*
    return 0; }

How about inheritance instead of composition?

struct Color {
    float Red;
    float Green;
    float Blue; };

struct DiffuseColor : public Color {
    };

struct Material : public DiffuseColor {
    float Brightness; };


int main() {
    float Material::* ParamToAnimate;
    ParamToAnimate = &Material::Brightness;       // Ok
    ParamToAnimate = &Material::DiffuseColor::Red; // Ok! *whew*
    return 0; }
标点 2024-09-20 16:02:04

您可以简单地进行重构,这样就根本没有嵌套结构。添加一个设置器,而不是将颜色解压到其组成部分中,以便现有代码不需要进行太多更改,然后从那里开始。

您还可以采用可选的第二个指针来挖掘嵌套类型。与当前方法相比,检查是否需要第二个参数的单个测试可能足够好,并且如果稍后出现其他字段,则更容易扩展。

更进一步,您将拥有一个带有虚拟 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 virtual Dereference 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 produce MaterialMember* 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.

时光磨忆 2024-09-20 16:02:04

由于在某些时候您需要一个指向实际数据的指针,因此这可能适合您,也可能不适合您:

float Material::* ParamToAnimate;
ParamToAnimate = &Material::Brightness;       // Ok
float Color::* Param2;
Param2 = &Color::Red; 

Material mat;
mat.Brightness = 1.23f;
mat.DiffuseColor.Blue = 1.0f;
mat.DiffuseColor.Green = 2.0f;
mat.DiffuseColor.Red = 3.0f;

float f = mat.DiffuseColor.*Param2;

Since at some point you need a pointer to the actual data, this may or may not work for you:

float Material::* ParamToAnimate;
ParamToAnimate = &Material::Brightness;       // Ok
float Color::* Param2;
Param2 = &Color::Red; 

Material mat;
mat.Brightness = 1.23f;
mat.DiffuseColor.Blue = 1.0f;
mat.DiffuseColor.Green = 2.0f;
mat.DiffuseColor.Red = 3.0f;

float f = mat.DiffuseColor.*Param2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文