gpt4 book ai didi

c++ - 指向 const 成员变量的指针

转载 作者:太空狗 更新时间:2023-10-29 23:42:23 26 4
gpt4 key购买 nike

我想知道指向常量成员变量的指针的语法是什么。

我知道指向非 const 成员函数的指针和指向 const 成员函数的指针是明显不同的类型,即以下是两种不同的类型:

typedef void (Foo::*Bar)(void);
typedef void (Foo::*ConstBar)(void) const;

我想知道对于非 const 和 const 成员变量的指针是否也可以这样说,即以下两种不同的类型,如果是,后者的语法是什么:

typedef int (Foo::*var);
typedef int (Foo::*constVar) const; // Not the correct syntax.

谢谢。

最佳答案

指向成员的指针的类型需要匹配成员的类型:

typedef       int (Foo::*var);  // pointer to a data member of type 'int'
typedef const int (Foo::*cvar); // pointer to a data member of type 'const int'

成员函数的 const 限定是其类型的一部分,就像返回类型是其类型的一部分一样。

关于c++ - 指向 const 成员变量的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4543883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com