Struct inside a struct, annonymous?

Discussion in 'C++' started by eselk@surfbest.net, Jun 7, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Kaz Kylheku Guest

    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?
     
    Kaz Kylheku, Jun 7, 2006
    #2
    1. Advertising

  3. 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
     
    Victor Bazarov, Jun 7, 2006
    #3
  4. Guest

    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, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mayer

    annonymous functions -- how to

    Mayer, May 4, 2005, in forum: Python
    Replies:
    14
    Views:
    464
    Dave Benjamin
    May 7, 2005
  2. Mark McIntyre

    C beginner: struct inside another struct...

    Mark McIntyre, Apr 21, 2006, in forum: C Programming
    Replies:
    5
    Views:
    675
    Yourko
    Apr 23, 2006
  3. monkeydragon

    Struct inside a struct

    monkeydragon, Dec 18, 2005, in forum: C++
    Replies:
    3
    Views:
    17,914
    Rolf Magnus
    Dec 20, 2005
  4. arnuld

    struct inside struct

    arnuld, Jul 23, 2011, in forum: C Programming
    Replies:
    5
    Views:
    744
    Kleuskes & Moos
    Jul 24, 2011
  5. none
    Replies:
    8
    Views:
    409
Loading...

Share This Page