访问对象的取消引用值的语法
Syntax for Accessing Dereferenced Values of an Object
在以下代码中:
#include <iostream>
class Foo{
public:
int *a, *b;
}
Foo::Foo(int x, int y) : a( new int ( x ) ) , b( new int ( y ) )
{
}
Foo test(1,2);
如果我想定制存储在指针变量a
中的取消引用的值,为什么要写:
std::cout << *test.a << std::endl; //method 1
而不是:
std::cout << test.(*a) << std::endl; //method 2
在我看来,我们正在访问a
的未引用值,即*a
,并且这是类Foo
的test
对象的成员变量,因此方法2在语法上感觉更正确。方法1(我认为是正确的方法(似乎取消了对整个test
对象的引用。
需要*test.a
而不是test.(*a)
的原因是a
本身不存在(从作用域的角度来看(。它是Foo
的成员,因此要访问Foo::a
,您需要test.a
。然后将*
应用于test.a
,因为test.a
是一个指针。
相关文章:
- 很好的语法来获取对向量/数组数据的大小引用?
- C++:取消引用十六进制值,有点语法问题
- 解释通过从函数引用返回数组的语法
- 将引用绑定到指针的语法是什么?(各种)
- 访问对象的取消引用值的语法
- 统一取消引用语法是否可行?
- 将此类传递给引用的正确语法是什么?
- 解压缩C++指针/引用语法
- 引用变量何时合适,为什么?你能解释一下实际的语法和位置吗?
- C++构造函数中的语法错误 - arugment 是对另一个类中的对象的引用
- C++指针和引用的语法
- C++处理取消引用语法的泛型类
- 是否可以通过从lambda的引用中返回T型对象,而无需使用尾随返回类型语法
- 以 qreal 引用作为参数的语法
- 利用值类型和引用的统一语法
- 用于在C++中返回引用的语法-与号
- Const引用Visual Studio和GCC之间的语法差异
- C++中的指针和引用变量语法顺序
- 为什么"universal references"具有与右值引用相同的语法?
- 语法错误:html 映射文件中的解析错误 - ogmaps 引用错误:找不到变量:GUnload