Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
void test(char * Buffer, int length)

{

struct x {

char a1;

int a2;

double d1;

};

struct y {

char a3;

int a4;

}

Buffer is parameter.
I want to declare this Buffer to depend on structure type x or y?
How do I do?

What I have tried:

During 2 hours I have code some programs to test it,
but failed.
Posted
Updated 15-Mar-16 11:08am
Comments
jeron1 15-Mar-16 14:26pm    
What do you mean 'depend on'?, maybe rephrase your question as it is unclear (at least to me).
Member 12330615 15-Mar-16 14:44pm    
Sorry,
I want to the buffer to declare of the type of x or y.
Buffers are received from another server with many types, so I want to manage it according to the each type.
[no name] 15-Mar-16 15:17pm    
A dirty solution: Pass the "type" of the buffer as extra Parameter (or at the beginning of the struct, maybe better) and according to the type make some "dirty cast".

You could use a union like:
C++
union foo
{
    struct x
    {
        char a1;
        int a2;
        double d1;
    };
    struct y
    {
        char a3;
        int a4;
    };
};

but that relies on you receiving some extra information that identifies which structure the data is in.
 
Share this answer
 
Comments
Arthur V. Ratz 21-Mar-16 3:09am    
Best answer using unions. +5.
If you mean you need to have a x buffer handler and an y buffer handler for example, then you could either provide two overloaded methods, one for each type, or you could write a generic method if the actions taken were the same regardless of which type it was, and constrain the generic type to x and y.
Looking at your example structs, I think you need overloaded methods.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900