c ++使用指针访问对象的成员 [英] c++ Accessing members of objects using pointers
问题描述
我有变量mode
,我在下面的行中声明该变量:
I have variable mode
, which I am declaring with the following line:
StatusRecord mode;
StatusRecord
是struct
,其中包含几个不同类型的变量.
StatusRecord
is a struct
which holds several variables of different types.
我现在想创建一个指向mode
的指针,并通过使用函数填充其属性/字段来向该指针填充一些数据.我尝试这样做,如下所示:
I now want to create a pointer to mode
, and populate that pointer with some data by using a function to populate its attributes/ fields. I have tried doing this as follows:
StatusRecord mode;
StatusRecord *modePtr = &mode;
DataStore->getModeData(*modePtr);
在这里,我声明了struct
变量,创建了一个指向它的指针,并使用getModeData()
函数填充了该指针.但是,我现在想使用我刚刚在条件语句中填充的struct ptr
的属性:
Here, I am declaring the struct
variable, creating a pointer to it, and populating that pointer using the getModeData()
function. However, I now want to use an attribute of the struct ptr
I've just populated in a conditional statement:
if(*modePtr->eraseSelect ==2){
...
}
但是我在这行上看到一个编译错误:
But I'm getting a compile error on this line that says:
错误:"*"的操作数必须是指针
Error: operand of '*' must be a pointer
这是否意味着eraseSelect
属性应该是指针以及'modePtr`?我该如何解决该错误?
Does this mean that the eraseSelect
attribute should be a pointer as well as 'modePtr`? How would I fix this error?
推荐答案
您无需在此处取消引用指针:
You dont need to dereference your pointer here:
if(modePtr->eraseSelect ==2){
...
}
这篇关于c ++使用指针访问对象的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!