通过指针算法调用结构的结构属性
call a struct attribute of a struct via pointer arithmetic
struct Test1 {
struct Test2 {
DWORD VA1 = 0;
DWORD VA2 = 0;
Test2(DWORD hp):VA1(hp) { }
} *Ppy[5];
Test1() {
for (int i = 0; i < 5; i++)
*(Ppy + i) = new Test2((DWORD)i+2);
}
~Test1() {
for (int i = 0; i < 5; i++)
delete *(Ppy + i);
}
};
void main() {
Test1*Als = new Test1;
for (int i = 0; i < 5; i++)
Als->Ppy[i]->VA2;
// doesn't work-> cout << Als->*(Ppy + i)->VA2 << endl;
delete Als;
}
你好
如何将整行(如果可能(或至少
Ppy[i]
转换为 指针样式/算术:Als->Ppy[i]->VA2
像这样的东西,但它不起作用:
Als->*(Ppy + i)->VA2
有没有办法使这更加复杂(不是
asm
深(?
而不是:
Als->Ppy[i]->VA2;
你可以写:
(*(Als->Ppy + i))->VA2;
- 指向数组的指针
Als->Ppy
- 指向数组元素的指针
Als->Ppy + i
- 数组元素的值为
*(Als->Ppy + i)
数组元素的值本身就是一个指针,在使用它指向结构的元素之前,您需要将上述表达式包装在括号中。
相关文章:
- C++概念:如何使用'concept'检查模板化结构的属性?
- "perf_event_attr"结构的"read_format"属性的选项到底是什么?
- 使用内存地址访问结构的属性值
- 通过指针算法调用结构的结构属性
- 无法在 unordered_set 中找到 () 结构属性
- 嵌套结构属性继承
- 在C++中循环数组时访问结构属性
- 为什么结构属性声明和初始化顺序的行为是这样的?
- gcc 显示结构属性为 0,但 clang 显示不同的值
- 使用 Spirit 将 std::vector<std::vector<double> 解析为结构属性
- 如何使用索引访问C 结构属性值
- 从外部函数访问外部结构属性
- 如何在C++中将结构属性转换为指针引用
- 如何通过字符串名称访问结构属性
- 提升灵气自定义句法属性(通过语义动作设置结构属性的特定成员)
- boost::spirit::qi具有相同的简单自适应结构属性的规则会导致编译错误
- 结构属性操作符重载
- 读取int*类型的结构属性值时遇到问题
- 模板递归中的函数、结构枚举和结构属性有什么区别?
- 在 C 中,除了使用 val 之外,不能更改结构属性 #define