什么是::*在C++中
What is ::* in C++?
当我面对时,我正在阅读一个基本的C++教程
::*
在以下代码中。我可以知道那是什么吗:
class A {
public:
protected:
int i;
};
class B : public A {
friend void f(A*, B*);
void g(A*);
};
void f(A* pa, B* pb) {
// pa->i = 1;
pb->i = 2;
// int A::* point_i = &A::i;
int A::* point_i2 = &B::i;
}
void B::g(A* pa) {
// pa->i = 1;
i = 2;
// int A::* point_i = &A::i;
int A::* point_i2 = &B::i;
}
void h(A* pa, B* pb) {
// pa->i = 1;
// pb->i = 2;
}
int main() { }
根据我目前对C++的了解,下面是什么?
int A::* point_i2
point_i2
是指向成员的指针。这意味着它指向在类A
中声明的int
成员变量。
int A::* point_i2 = &B::i;
之后,当您有一个随机的A
或B
对象时,您可以访问point_i2
指向的成员
B b;
b.*point_i2 = ...;
在point_i2
的上述初始化之后,这将改变b.i
。
以与&
和*
相同的方式思考ClassName::*
:它只是另一个"类似指针/引用的工具",您可以在声明中使用它来指定您声明的内容
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 我应该使用什么来代替void作为变体中的替代类型之一
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用