存储相关结构 [英] storing related structs

查看:76
本文介绍了存储相关结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个相关的结构:

struct A

{

int x;

void * data ;

};





struct B

{

int x,y;

double z;

void * data1,* data2;

};

这些结构(不是它们的指针),需要存储在另一个

结构中:


struct C

{

unsigned id;

char name;


DATA_TYPE the_struct;

};

其中DATA_TYPE是结构A或结构B。


为了与现有库兼容,我不能使用void *(对于

实例),并在结构A和B之间进行转换。

是否有解决此问题的方法?

解决方案



灰色外星人 < gr ** @ andromeda.comha scritto nel messaggio新闻:Z7 ****************************** @ bt.com。 ..


>我有两个相关的结构:


struct A

{

int x;

void * data;

};





struct B

{

int x,y;

double z;

void * data1,* data2;

};


这些结构(不是它们的指针)需要存储在另一个结构中:


struct C

{

unsigned id;

char name;


DATA_TYPE the_struct;

};


其中DATA_TYPE是结构A或结构B。


为了与现有库兼容,我不能使用void *(例如),并在struct A和B之间进行转换。


这个问题有解决方法吗?



struct C {

unsigned id;

char name; / *你的意思是*姓名,还是名字[LARGE_ENOUGH]? * /

enum {strA,strB} type;

union {

struct A str_A;

struct B str_B;

} the_struct;

};


联合就像一个结构,但只能保存一个元素时间。

您可以像结构成员一样访问工会成员。例如,

在上面的例子中访问struct C中的struct B的两倍,

你将不得不使用mystructC.the_struct.str_B.z

或者试着找一个更好的方法来做你想做的事。


6月30日,13:15, ; Army1987" < please .... @ for.itwrote:


" Gray Alien" < g ... @ andromeda.comha scritto nel messaggionews:Z7 ****************************** @ bt。 com ...


我有两个相关的结构:


struct A

{

int x;

void * data;

};




struct B

{

int x,y;

double z;

void * data1,* data2;

} ;


这些结构(不是它们的指针)需要存储在另一个结构中:


struct C

{

unsigned id;

char name;


DATA_TYPE the_struct;

};


其中DATA_TYPE是结构A或结构B.


为了与现有库兼容,我不能使用void *(例如),并在struct A和B之间进行强制转换。


是否有soln这个问题?



struct C {

unsigned id;

char name; / *你的意思是*姓名,还是名字[LARGE_ENOUGH]? * /

enum {strA,strB} type;

union {

struct A str_A;

struct B str_B;

} the_struct;


};


联合就像一个结构,但只能持有一次一个元素。

您可以像结构成员那样访问工会成员。例如,

在上面的例子中访问struct C中的struct B的两倍,

你将不得不使用mystructC.the_struct.str_B.z

或者试着找到一种更好的方法去做你想做的事情。



对于OP:一种方法是使用一个联盟,正如Army1987

所建议的那样。但是,如果空间不是问题,你可能会做得更好

,包括结构A和结构B,并且只使用你想要的那个

。这似乎不太可能出现可怕的错误,

并且可能会使您的代码更清晰。是否有一些理由为什么这将无法工作?


希望有所帮助。

保罗。


Gray Alien写道,On 30/06/07 12:28:


我有两个相关的结构:


struct A



< snip>


struct B



< snip>


这些结构(不是它们的指针)需要存储在另一个

结构中:


struct C

{

unsigned id;

char name;

DATA_TYPE the_struct;

};


其中DATA_TYPE是结构A或结构B.


为了与现有库兼容,我不能使用void *(用于

实例),并在struct A和B之间进行转换。


这个问题有解决方法吗?



是的,使用两种结构类型的并集。显然你需要一些东西

来表明存储的内容,以便你知道如何访问它。

-

Flash Gordon


I have two related structs:
struct A
{
int x ;
void * data ;
};

and

struct B
{
int x, y ;
double z ;
void * data1, *data2 ;
};
These structures (not pointers to them), need to be stored in a another
struct:

struct C
{
unsigned id ;
char name ;

DATA_TYPE the_struct ;
};
Where DATA_TYPE is EITHER a struct A or struct B.

For compatability with existing libraries, I cannot use a void * (for
instance), and cast between struct A and B.
Is there a soln to this problem ?

解决方案


"Gray Alien" <gr**@andromeda.comha scritto nel messaggio news:Z7******************************@bt.com...

>I have two related structs:
struct A
{
int x ;
void * data ;
};

and

struct B
{
int x, y ;
double z ;
void * data1, *data2 ;
};
These structures (not pointers to them), need to be stored in a another struct:

struct C
{
unsigned id ;
char name ;

DATA_TYPE the_struct ;
};
Where DATA_TYPE is EITHER a struct A or struct B.

For compatability with existing libraries, I cannot use a void * (for instance), and cast between struct A and B.
Is there a soln to this problem ?

struct C {
unsigned id;
char name; /* do you mean *name, or name[LARGE_ENOUGH]? */
enum { strA, strB } type;
union {
struct A str_A;
struct B str_B;
} the_struct;
};

A union is like a struct, but can only hold an element at a time.
You access members of unions like members of struct. For example,
in the case above to access the double of a struct B in a struct C,
you''ll have to use mystructC.the_struct.str_B.z
Or try to find a better way to do what you''re trying to do.


On 30 Jun, 13:15, "Army1987" <please....@for.itwrote:

"Gray Alien" <g...@andromeda.comha scritto nel messaggionews:Z7******************************@bt. com...

I have two related structs:

struct A
{
int x ;
void * data ;
};

and

struct B
{
int x, y ;
double z ;
void * data1, *data2 ;
};

These structures (not pointers to them), need to be stored in a another struct:

struct C
{
unsigned id ;
char name ;

DATA_TYPE the_struct ;
};

Where DATA_TYPE is EITHER a struct A or struct B.

For compatability with existing libraries, I cannot use a void * (for instance), and cast between struct A and B.

Is there a soln to this problem ?


struct C {
unsigned id;
char name; /* do you mean *name, or name[LARGE_ENOUGH]? */
enum { strA, strB } type;
union {
struct A str_A;
struct B str_B;
} the_struct;

};

A union is like a struct, but can only hold an element at a time.
You access members of unions like members of struct. For example,
in the case above to access the double of a struct B in a struct C,
you''ll have to use mystructC.the_struct.str_B.z
Or try to find a better way to do what you''re trying to do.

To the OP: One way of doing it would be to use a union, as Army1987
has suggested. However, if space isn''t an issue, you might do better
including both a struct A and a struct B, and just using the one that
you want. This would seem to have less chance of going horribly wrong,
and might make your code clearer. Is there some reason why this would
not work?

Hope that helps.
Paul.


Gray Alien wrote, On 30/06/07 12:28:

I have two related structs:

struct A

<snip>

struct B

<snip>

These structures (not pointers to them), need to be stored in a another
struct:

struct C
{
unsigned id ;
char name ;
DATA_TYPE the_struct ;
};
Where DATA_TYPE is EITHER a struct A or struct B.

For compatability with existing libraries, I cannot use a void * (for
instance), and cast between struct A and B.

Is there a soln to this problem ?

Yes, use a union of the two struct types. Obviously you need something
to indicate what is stored so you know how to access it.
--
Flash Gordon


这篇关于存储相关结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆