Struct inside a struct, annonymous?

E

eselk

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.
 
K

Kaz Kylheku

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?
 
V

Victor Bazarov

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
 
E

eselk

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top