public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Kai Meyer <kai@gnukai.com>
To: gcc-help@gcc.gnu.org
Subject: Re: C Struct inheritance?
Date: Fri, 17 Jun 2011 23:06:00 -0000	[thread overview]
Message-ID: <4DFB891D.7040509@gnukai.com> (raw)
In-Reply-To: <4DFB81BC.9040208@gnukai.com>

After mailing the list, I showed a co-worker my post, and he spent all 
of 15 minutes tracking it down. MS extensions call it "Anonymous Structs":
http://msdn.microsoft.com/en-us/library/z2cx9y4f.aspx

GCC has a -fms-extensions option, which does then allow this behavior. 
Hurray! It appears to give me what I need for now.

-Kai Meyer

On 06/17/2011 10:33 AM, Kai Meyer wrote:
> I'm basically straight out of acadamia with a 4 year CS degree, so be 
> gentle :)
>
> I've been working adding a Linux port to a current Windows-only 
> project, and I've run into what appears to be inheritance with C 
> structs. I've distilled the problem down to the following code:
>
> // Test for struct inheritance
> #ifdef __cplusplus
> struct base
> #else
> typedef struct _base
> #endif
> {
> int b;
> #ifdef __cplusplus
> };
> #else
> } base;
> #endif
> #ifdef __cplusplus
> struct derived : public base
> {
> #else
> typedef struct _derived
> {
> base;
> #endif
> int d;
> #ifdef __cplusplus
> };
> #else
> } derived;
> #endif
> int main()
> {
> base b;
> derived d;
> b.b = 5;
> d.b = b.b;
> d.d = 10;
> return 0;
> }
>
> It is apparent that the struct is to behave the same whether compiled 
> in C or C++. I admit to being a bit confused to see Polymorphism in C. 
> This code builds and runs in my WinDDK environment for windows, as 
> well as for Linux g++, but Linux gcc gives me the following error:
> struct_test.c:20: warning: declaration does not declare anything
> struct_test.c: In function ‘main’:
> struct_test.c:33: error: ‘derived’ has no member named ‘b’
>
> That totally makes sense. The C I know doesn't have polymorphism.
>
> So, I need to derive a solution that will be cross-platform, and 
> hopefully not require any changes to existing code that uses these 
> structs. I'm sort of holding my breath for a gcc option like 
> "-magic_structs". Any help or general advice is welcome.

      reply	other threads:[~2011-06-17 17:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-17 17:33 Kai Meyer
2011-06-17 23:06 ` Kai Meyer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4DFB891D.7040509@gnukai.com \
    --to=kai@gnukai.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).