# Structures

Is there any way to pass a reference to a structure without knowing what type of structure it is? For example:

[code]
struct OneStruct
{
int One;
int Two;
}onestruct;

struct TwoStruct
{
int Three;
int Four;
}twostruct;
[/code]

Is there a pointer type that can hold a reference to one of these structures without knowing which one it is? Let's say I wanted one function to deal with both types:

[code]
void MyFunc()
{
//Do whatever
}
[/code]

Assuming I had a parameter to tell me which type of structure was being passed to it (e.g. an integer value), is there another parameter that would accept any reference? I tried to use void *, however I can't access the structure members with that. So is that possible within a single function, or would I need a separate function for each structure type?

Apologies if I haven't expressed myself clearly enough.

## Comments

• : Is there any way to pass a reference to a structure without knowing what type of structure it is? For example:
:
: [code]
: struct OneStruct
: {
: int One;
: int Two;
: }onestruct;
:
: struct TwoStruct
: {
: int Three;
: int Four;
: }twostruct;
: [/code]
:
: Is there a pointer type that can hold a reference to one of these structures without knowing which one it is? Let's say I wanted one function to deal with both types:
:
: [code]
: void MyFunc()
: {
: //Do whatever
: }
: [/code]
:
: Assuming I had a parameter to tell me which type of structure was being passed to it (e.g. an integer value), is there another parameter that would accept any reference? I tried to use void *, however I can't access the structure members with that. So is that possible within a single function, or would I need a separate function for each structure type?
:
: Apologies if I haven't expressed myself clearly enough.
:

I think I might use a union of the two structures
: [code]
union outer_struct
{
struct OneStruct
{
int One;
int Two;
}onestruct;

struct TwoStruct
{
int Three;
int Four;
}twostruct;
};
[/code]

since both structures in that union occupy the same space at the same time you cannot use them both at the same time -- only one or the other. Since both structures have identical size and contents, changing onestruct.One will make an identical change to twostruct.Three.

The program that uses that union will have to be aware of this structure it needs to use.

• If you work in C++ you can make base struct (class) and from him derive structures

struct BaseStruct {}

struct OneStruct : public BaseStruct
{
...
} onestruct;
struct TwoStruct : public BaseStruct
{
...
}twostruct;

void MyFunc(BaseStruct mystruct)
{
...
}

But you must known what type you are using before casting into this type.
With exactly structure it work fine

void MyFunc(BaseStruct& s)
{
OneStruct* one = static_cast(&s);
std::cout << one->One << std::endl;
std::cout << one->Two << std::endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
onestruct.One = 1;
onestruct.Two = 2;
twostruct.Four = 4;
twostruct.Three = 3;

MyFunc(onestruct);
MyFunc(twostruct);

return 0;
}

output:
1
2
3
4

but if you have diferent structures, then it works bad.
You can use void (to save pointer to structures), but before call member of structure you need casting to structure type.

: Is there any way to pass a reference to a structure without knowing what type of structure it is? For example:
:
: [code]
: struct OneStruct
: {
: int One;
: int Two;
: }onestruct;
:
: struct TwoStruct
: {
: int Three;
: int Four;
: }twostruct;
: [/code]
:
: Is there a pointer type that can hold a reference to one of these structures without knowing which one it is? Let's say I wanted one function to deal with both types:
:
: [code]
: void MyFunc()
: {
: //Do whatever
: }
: [/code]
:
: Assuming I had a parameter to tell me which type of structure was being passed to it (e.g. an integer value), is there another parameter that would accept any reference? I tried to use void *, however I can't access the structure members with that. So is that possible within a single function, or would I need a separate function for each structure type?
:
: Apologies if I haven't expressed myself clearly enough.
:

Sign In or Register to comment.

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!