Author |
Topic |
|
jackhab
New Member
6 Posts |
Posted - Jan 09 2008 : 08:40:03 AM
|
With the following code:
typedef struct test1 {
int a;
int b;
}TEST1;
struct test2 {
TEST1;
int c;
}tester2;
referencing to tester2 does not list a and b members.
|
|
feline
Whole Tomato Software
United Kingdom
19024 Posts |
Posted - Jan 09 2008 : 11:15:51 AM
|
using VS2005 and VA 1624, C++, this code does not compile. Is your sample correct?
I can see what you are trying to do here, but I don't think I have ever seen it done quite like this. |
zen is the art of being at one with the two'ness |
|
|
jackhab
New Member
6 Posts |
Posted - Jan 10 2008 : 03:19:49 AM
|
I'm compiling a C file with: #include <stdio.h>
typedef struct test1 { int a; int b; }TEST1;
struct test2 { TEST1; int c; };
int main (int argc, char *argv[]) { struct test2 st; st.a = 1; st.b = 2; st.c = 3; printf("%d %d %d", st.a, st.b, st.c); return 0; }
It compiles and runs OK. I also tried: struct test2 { struct test1; int c; }; with no problems.
What I'm trying to do is to extend test1 structure with additional variables (kind of plain C "inheritance"). If you know any other way to do it, please, let me know.
Thanks. |
|
|
feline
Whole Tomato Software
United Kingdom
19024 Posts |
Posted - Jan 10 2008 : 08:58:27 AM
|
The code compiles quite happily in a .c file, but not in a .cpp file. I was under the impression C++ was a super set of C, but apparently not quite.
Is there some reason why you are not compiling this as C++? Since you are working inside one of the Visual Studio IDE's you have a C++ compiler available to you, so you could use C++ inheritance.
You might find these two forum threads:
http://forum.wholetomato.com/forum/topic.asp?TOPIC_ID=7134 http://forum.wholetomato.com/forum/topic.asp?TOPIC_ID=6849
useful, since they are looking at a related problem.
To be honest I have never tried to do this in C, so I am not quite sure how else to do it. I can see what you are doing, but I was not aware this was valid code. |
zen is the art of being at one with the two'ness |
|
|
jackhab
New Member
6 Posts |
Posted - Jan 10 2008 : 10:52:03 AM
|
Probably it is illegal after all. When I tried to compiled it with GCC it gave me 'has no member' errors.
The reason I'm not using C++ is that I'm developing PowerPC firmware and VS is just an editor for me.
Thanks for the links. |
|
|
feline
Whole Tomato Software
United Kingdom
19024 Posts |
Posted - Jan 10 2008 : 1:57:56 PM
|
*ah* well that answers the language question.
You might be able to trick / fool VA into working out what is going on here. As a guess VA might work this out:
#define VA_HELPER_CODE
#define TEST1_STRUCT typedef struct test1 { int a; int b; };
#else
typedef struct test1 {
int a;
int b;
}TEST1;
#endif
struct test2 {
#define VA_HELPER_CODE
TEST1_STRUCT;
#else
TEST1;
#endif
int c;
};
Its not very "clean" though. If something like this works you might be able to simply it a bit. |
zen is the art of being at one with the two'ness |
|
|
jackhab
New Member
6 Posts |
Posted - Jan 13 2008 : 06:30:46 AM
|
I checked this issue more thoroughly. The unnamed structures are supported by both MS and GCC compilers. I had a mistake in my previous example code, when GCC did not compile it. The nested structure should have been declared as anonymous.
From MSDN article C Language Reference, Structure Declarations
quote: Structure declarations can also be specified without a declarator when they are members of another structure or union. The field names are promoted into the enclosing structure. For example, a nameless structure looks like this:
struct s
{
float y;
struct
{
int a, b, c;
};
char str[10];
} *p_s;
.
.
.
p_s->b = 100; /* A reference to a field in the s structure */
Seems like VAX does handle anonymous structures, but it propagates the names only one level up:
struct {
union {
Uint8 V6[16];
Uint8 V4[4];
};
SMP_BOOL IsIpv4;
}Ip;
The members of the above structure are listed OK: V6, V4, IsIpv4. While for the following structure:
struct{
union {
Uint8 V6[16];
struct {
Uint8 __padding[12];
Uint8 V4[4];
};
};
BOOL IsIpv4;
}Ip;
only V6 and IsIpv4 listed. Is it a VAX bug? |
|
|
feline
Whole Tomato Software
United Kingdom
19024 Posts |
Posted - Jan 14 2008 : 08:12:57 AM
|
VA not understanding, listing, the members of the anonymous structure is indeed a VA bug:
case=4514
As discussed in this thread: http://forum.wholetomato.com/forum/topic.asp?TOPIC_ID=7134
the current workaround is to give the anonymous structure a name. This code works, without changing the actual structure declaration as seen by the compiler:
#ifdef CHEAT_TO_HELP_VA
#define ANONYMOUS_STRUCT ANONYMOUS_STRUCT
#else
#define ANONYMOUS_STRUCT
#endif
struct{
union {
Uint8 V6[16];
struct ANONYMOUS_STRUCT {
Uint8 __padding[12];
Uint8 V4[4];
};
};
BOOL IsIpv4;
}Ip; |
zen is the art of being at one with the two'ness |
|
|
jackhab
New Member
6 Posts |
Posted - Jan 15 2008 : 11:10:02 AM
|
Thanks! Is there any workaround if I need to typedef such a structure? It's not working with typedef.
typedef struct
{
union
{
Uint8 V6[16];
struct
{
Uint8 V4[4];
Uint8 __padding[12];
};
};
BOOL IsIpv4;
}IPV6V4ADDRESS;
IPV6V4ADDRESS IpAddress;
|
|
|
feline
Whole Tomato Software
United Kingdom
19024 Posts |
Posted - Jan 15 2008 : 1:50:30 PM
|
This code works for me, listing the correct items:
typedef struct
{
union
{
Uint8 V6[16];
struct ANONYMOUS_STRUCT
{
Uint8 V4[4];
Uint8 __padding[12];
};
};
BOOL IsIpv4;
}IPV6V4ADDRESS;
IPV6V4ADDRESS IpAddress;
static void testMemberList()
{
IPV6V4ADDRESS foo;
foo.|;
}
|
zen is the art of being at one with the two'ness |
|
|
support
Whole Tomato Software
5566 Posts |
Posted - Apr 24 2008 : 12:26:48 AM
|
case=4514 is fixed in build 1635 |
|
|
|
Topic |
|