473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Struct inside a struct, annonymous?

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.

Jun 7 '06 #1
3 3665
es***@surfbest.net 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
Jun 7 '06 #2
es***@surfbest.net 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?

Jun 7 '06 #3
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.


Jun 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Mayer | last post by:
Hello: I would like to define a very large annonymous function, one with several statements in sequence. I know how to define annonymous functions, but I don't know how to define a sequence of...
1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
4
by: James Roberge | last post by:
I am having a little trouble getting my union/struct to work correctly. I am creating a struct that will contain information about the status of various Z80 cpu registers in an emulator i am...
20
by: Elliot Marks | last post by:
If a struct or its members are passed to a function, must it be declared globally? #include <stdio.h> struct mystruct{ int a; int b; }; int structfunc(struct mystruct foo);
1
by: Jón Sveinsson | last post by:
Hello everone I have code from c which I need to convert to c#, the c code is following, as you can see there is a struct inside struct typedef struct { some variables....
6
by: Michael C | last post by:
Is it possible to use an ArrayList inside a struct? I keep running into a null reference exception when I try to Add to the ArrayList in the struct, and it won't let me initialize the ArrayList in...
6
by: CptDondo | last post by:
I'm trying to figure out some code that uses structures, structures, and more structures... It's a bit of rat's nest, and I'm having some trouble sorting it all out. The authors use a lot of...
12
by: djhong | last post by:
Following is a snippet of a header file. Here, #define are inside struct evConn{} Any advantage of putting them inside struct block? Looks like there is no diff in scoping of each...
3
by: dreiko466 | last post by:
(sorry about my english...) I am a newbie in C (3 month expierience) I have wrote a simple test programm in VS2005, what i do wrong?Please... In this programm i create a double linked list.Then ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.