From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5848 invoked by alias); 17 Jun 2011 17:04:45 -0000 Received: (qmail 5839 invoked by uid 22791); 17 Jun 2011 17:04:44 -0000 X-SWARE-Spam-Status: No, hits=-0.4 required=5.0 tests=AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from host-6.srv-69-160-84.legion-1.nethosting.com (HELO mailhost.gnukai.com) (69.160.84.6) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 17 Jun 2011 17:04:30 +0000 Received: by mailhost.gnukai.com (Postfix, from userid 508) id 43318855EAC; Fri, 17 Jun 2011 11:04:30 -0600 (MDT) Received: from kai-rhel6.stc.local (unknown [216.194.115.194]) by mailhost.gnukai.com (Postfix) with ESMTP id D7CC5855EAB for ; Fri, 17 Jun 2011 11:04:29 -0600 (MDT) Message-ID: <4DFB891D.7040509@gnukai.com> Date: Fri, 17 Jun 2011 23:06:00 -0000 From: Kai Meyer User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110419 Red Hat/3.1.10-1.el6_0 Thunderbird/3.1.10 MIME-Version: 1.0 To: gcc-help@gcc.gnu.org Subject: Re: C Struct inheritance? References: <4DFB81BC.9040208@gnukai.com> In-Reply-To: <4DFB81BC.9040208@gnukai.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2011-06/txt/msg00278.txt.bz2 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.