Pointers 铸造空心指针
我有一个结构Pointers 铸造空心指针,pointers,struct,casting,Pointers,Struct,Casting,我有一个结构 struct GROUP_POINTS { unsigned char number_of_points; void *points; }; struct GROUP_POINTS group_points; 点是空指针的原因是,我希望使组尽可能通用,并在运行时将“链接”设置为正确的结构 另一个结构是: struct POINT_A { unsigned char something; }; 我可以创建另一个指向*点的指针来访问结构,如: struct PO
struct GROUP_POINTS
{
unsigned char number_of_points;
void *points;
};
struct GROUP_POINTS group_points;
点是空指针的原因是,我希望使组尽可能通用,并在运行时将“链接”设置为正确的结构
另一个结构是:
struct POINT_A
{
unsigned char something;
};
我可以创建另一个指向*点的指针来访问结构,如:
struct POINT_A *point_a = (struct POINT_A *)group_points.points;
然后通过执行以下操作访问这些点:
(*point_a).number_of_points = 5;
但我真的希望能够像这样使用它:
group_points.points.number_of_points
((struct POINT_A *)group_points).points.number_of_points = 5;
所以不需要第二个指针,只需要指向void指针。有没有办法做到这一点假设语言是C++,你可能想考虑模板解决方案:
template <class T>
struct GROUP_POINTS
{
unsigned char number_of_points;
T *points;
};
typedef GROUP_POINTS<unsigned char> POINT_A;
//another typedefs for another points.
模板
结构组_点
{
点的无符号字符数;
T*点;
};
typedef GROUP_POINTS_POINTS_A;
//另一个类型定义另一个点。
此外,您可能只需要使用std::vector而不是整点结构就可以了,但为了说明一般方法,这就是如何实现的。由于您只需要避免使用另一个指针,您可以这样使用它:
group_points.points.number_of_points
((struct POINT_A *)group_points).points.number_of_points = 5;
注意,类型CAST的优先级低于
但是inta=42;双b=(双)a
的类型仍然是a
。明白了,这就是我所想但不希望的:)谢谢所有的帮助! int