在 C++ 中取消引用指向结构的指针
Derefencing a pointer to a struct in C++
我是 C++ 的新手,正在尝试弄清楚如何取消引用指向结构的指针,以便我可以检索该结构中成员的值。在 buildCar() 中,我希望能够打印汽车的年份以及汽车的规格(设置后,将是指向另一个结构的指针)。我能够打印出存储 car1 结构的内存地址,但我无法找出用于引用此指针的正确语法。我试过使用 (*) 来取消引用,并且 -> 而不是 .引用结构的成员,但到目前为止还没有运气。任何帮助表示赞赏。谢谢!
struct item {
int id;
void *myData;
};
struct car {
unsigned int year;
char* specs;
};
void buildCar(item item1);
int main() {
struct item item1;
item1.id = 1;
struct car car1;
car1.year = 2019;
item1.myData = &car1;
buildCar(item1);
}
void buildCar(item item1){
cout << "address of car1 = " << item1.myData;
}
I'm having trouble figuring out the correct syntax to use to deference this pointer.
您不能通过 void 指针间接访问。您只能通过 non-void 指针间接访问。
解决方法:改变指针的类型:
struct item {
int id;
struct car *myData;
};
然后您可以使用间接成员访问运算符 ->
.
访问指向 object 的成员
Presumably there could be other types of items than cars, so a void * might be necessary.
在纯C++中,根据使用需求,有继承、std::variant
、std::any
等更好的选择
鉴于 Mango 在评论中提到 header 是 C,可能没有更好的替代方案,void*
可能确实是必要的。
在这种情况下,void 指针可以转换回使用 static_cast
转换成 void 指针的原始指针类型。转换为错误类型的行为将是未定义的,因此必须非常小心。
那么,让我们从一个基础开始:在 C++ 中,结构是 classes,默认为 public。
你的语言看起来像 C,所以我假设这是你的基础语言。
这意味着您可以访问结构中 class 行为配置的完整范围。
因此你可以重写它:
struct Car {
unsigned int year;
char* specs;
Car(int year) : year(year) {} //Constructor
};
struct Item {
int id;
Car *myData;
Item(int id, Car* car) : id(id), myData(car) {} // Constructor
~Item() { delete myData; } // Destructor. This prevents a potential data leak.
void buildCar() { std::cout << "address of car1 = " << myData; } // Member Function
};
int main() {
Item item1(1, new Car(2019));
item1.buildCar();
}
如果您想知道其中的一些东西是什么,请查找构造函数语法、构造函数初始值设定项列表和 new
运算符,它们对 C++ 来说非常重要。
我是 C++ 的新手,正在尝试弄清楚如何取消引用指向结构的指针,以便我可以检索该结构中成员的值。在 buildCar() 中,我希望能够打印汽车的年份以及汽车的规格(设置后,将是指向另一个结构的指针)。我能够打印出存储 car1 结构的内存地址,但我无法找出用于引用此指针的正确语法。我试过使用 (*) 来取消引用,并且 -> 而不是 .引用结构的成员,但到目前为止还没有运气。任何帮助表示赞赏。谢谢!
struct item {
int id;
void *myData;
};
struct car {
unsigned int year;
char* specs;
};
void buildCar(item item1);
int main() {
struct item item1;
item1.id = 1;
struct car car1;
car1.year = 2019;
item1.myData = &car1;
buildCar(item1);
}
void buildCar(item item1){
cout << "address of car1 = " << item1.myData;
}
I'm having trouble figuring out the correct syntax to use to deference this pointer.
您不能通过 void 指针间接访问。您只能通过 non-void 指针间接访问。
解决方法:改变指针的类型:
struct item {
int id;
struct car *myData;
};
然后您可以使用间接成员访问运算符 ->
.
Presumably there could be other types of items than cars, so a void * might be necessary.
在纯C++中,根据使用需求,有继承、std::variant
、std::any
等更好的选择
鉴于 Mango 在评论中提到 header 是 C,可能没有更好的替代方案,void*
可能确实是必要的。
在这种情况下,void 指针可以转换回使用 static_cast
转换成 void 指针的原始指针类型。转换为错误类型的行为将是未定义的,因此必须非常小心。
那么,让我们从一个基础开始:在 C++ 中,结构是 classes,默认为 public。
你的语言看起来像 C,所以我假设这是你的基础语言。
这意味着您可以访问结构中 class 行为配置的完整范围。
因此你可以重写它:
struct Car {
unsigned int year;
char* specs;
Car(int year) : year(year) {} //Constructor
};
struct Item {
int id;
Car *myData;
Item(int id, Car* car) : id(id), myData(car) {} // Constructor
~Item() { delete myData; } // Destructor. This prevents a potential data leak.
void buildCar() { std::cout << "address of car1 = " << myData; } // Member Function
};
int main() {
Item item1(1, new Car(2019));
item1.buildCar();
}
如果您想知道其中的一些东西是什么,请查找构造函数语法、构造函数初始值设定项列表和 new
运算符,它们对 C++ 来说非常重要。