Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Struct inside a struct, annonymous?

Reply
Thread Tools

Struct inside a struct, annonymous?

 
 
eselk@surfbest.net
Guest
Posts: n/a
 
      06-07-2006
I have a struct defined like this:

typedef struct
{
int file_handle;
int x;
int y;
} BaseFiles;

I'd basicly like to "sub-class" this structure so that I can add a
couple of members to the end of it, but still use the BaseFiles members
when it is typecast to a BaseFiles pointer.

This works with the Borland C compiler, but the C++ compiler doesn't
like it.

typedef struct
{
BaseFiles;
int z;
} Files3D;

I can then use this code:

Files3D f;
f.x = 10;
f.y = 20;
f.z = 30;

And something like this (but less ugly) works also:

((BaseFiles*)&f)->x = 10;

Is there any way to achieve the same thing in C++? From the help file
I know this exact method isn't supported:

"A Microsoft extension to the C programming language allows the use of
a tagged structure definition within an enclosing structure. The
declaration in the enclosing structure need not name a variable. The
Borland C compiler supports this extension. [snip] This applies only to
Borland C; it is illegal in both ANSI and Borland C++."

For now I have this:

typedef struct
{
int file_handle;
int x;
int y;
int z;
} Files3D;

Which works, but if BaseFiles ever changes, I have to change Files3D
also, or I could end up with bugs.

Keep in mind I have lots of legacy code that uses BaseFiles. Just
looking for a simple way to do this, if possible, but if it requires
changing a bunch of my other code, I'll stick with the kludge or maybe
just add the extra member to the BaseFiles struct.

 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      06-07-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have a struct defined like this:
>
> typedef struct
> {
> int file_handle;
> int x;
> int y;
> } BaseFiles;


In C++, you write it like this:

struct BaseFiles {
// ...
};

No typedef is needed. Now BaseFiles is a class name.

> I'd basicly like to "sub-class" this structure so that I can add a
> couple of members to the end of it, but still use the BaseFiles members
> when it is typecast to a BaseFiles pointer.


This is done in C++ like this:

struct DerivedFiles : public BaseFiles {
// members go here
};

> typedef struct
> {
> BaseFiles;
> int z;
> } Files3D;
>
> I can then use this code:
>
> Files3D f;
> f.x = 10;
> f.y = 20;
> f.z = 30;
>
> And something like this (but less ugly) works also:
>
> ((BaseFiles*)&f)->x = 10;
>
> Is there any way to achieve the same thing in C++?


Doh?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-07-2006
(E-Mail Removed) wrote:
> I have a struct defined like this:
>
> typedef struct
> {
> int file_handle;
> int x;
> int y;
> } BaseFiles;


So, that's an anonymous struct for which you defined an alias,
'BaseFiles'. Why don't you use the normal C++ way? How about
you begin by changing this to

struct BaseFiles
{
int file_handle;
int x;
int y;
};

> I'd basicly like to "sub-class" this structure so that I can add a
> couple of members to the end of it, but still use the BaseFiles
> members when it is typecast to a BaseFiles pointer.
>
> This works with the Borland C compiler, but the C++ compiler doesn't
> like it.
>
> typedef struct
> {
> BaseFiles;


What is that supposed to do? AFAIK, it's a syntax error in C++.

> int z;
> } Files3D;
>
> I can then use this code:
>
> Files3D f;
> f.x = 10;
> f.y = 20;
> f.z = 30;
>
> And something like this (but less ugly) works also:
>
> ((BaseFiles*)&f)->x = 10;


That's not C++. In C++ this produces undefined behaviour, AFAICT.

> Is there any way to achieve the same thing in C++?


Yes, it's called "inheritance".

> From the help file
> I know this exact method isn't supported:
>
> "A Microsoft extension to [..]"


Yes, well, we don't discuss extensions here. Sorry.

> For now I have this:
>
> typedef struct
> {
> int file_handle;
> int x;
> int y;
> int z;
> } Files3D;
>
> Which works, but if BaseFiles ever changes, I have to change Files3D
> also, or I could end up with bugs.


That's why you should do

struct BaseFiles
{
int file_handle;
int x;
int y;
};

struct Files3D : BaseFiles
{
int z;
};


> Keep in mind I have lots of legacy code that uses BaseFiles. Just
> looking for a simple way to do this, if possible, but if it requires
> changing a bunch of my other code, I'll stick with the kludge or maybe
> just add the extra member to the BaseFiles struct.


If you're porting (and that's what you should be doing), then you just
need to make sure the rest of the code compiles, and fix the code if it
does not (or /where/ it does not). If you need both types to work in
both C *and* C++, you're most likely out of luck AFA *portable* and
*standard* C++ is concerned.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
eselk@surfbest.net
Guest
Posts: n/a
 
      06-08-2006
Not porting, just updating an existing program which is about 40% C,
50% C++, and 10% pascal. I don't see anything wrong with using several
languages and making calls into C modules from C++, and using structs
as params. Heck, that is what any C++ program that uses the Windows
API does.

I can probably use an #ifdef in the header file to use one format for
C, and the other that you mentioned for C++. Don't see any reason that
wouln't work, as long as the data members are in the same order, and
byte packing is the same (everything in the same place in memory). So
I'll use embedded annonymous structs in C, and inheritance in C++, but
they both produce the same memory structure which is what I need.

Thanks for the syntax for struct inheritance, that is what I was
missing!

Victor Bazarov wrote:
> If you're porting (and that's what you should be doing), then you just
> need to make sure the rest of the code compiles, and fix the code if it
> does not (or /where/ it does not). If you need both types to work in
> both C *and* C++, you're most likely out of luck AFA *portable* and
> *standard* C++ is concerned.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
struct inside struct arnuld C Programming 5 07-24-2011 10:34 AM
C beginner: struct inside another struct... Mark McIntyre C Programming 5 04-22-2006 11:05 PM
Struct inside a struct monkeydragon C++ 3 12-20-2005 05:36 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments