Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8813683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:55:06+00:00 2026-06-14T03:55:06+00:00

Background: We’re implementing a dynamic library in C++ that extends the functionality of a

  • 0

Background: We’re implementing a dynamic library in C++ that extends the functionality of a C program. For one of the main structs used in the C program, we would like to add our own library specific fields. Currently when we need a new field, we ask nicely that the developers of the C program add a field for us and we end up having a massive casting mess. I was wondering if we could instead do the following:

Header file of main program:

#ifdef __cplusplus
extern "C" {
#endif
/* ... */
typedef struct ImportantStruct {
/* Definitions */
} ImportantStruct
/* ... */
#ifdef __cplusplus
}
#endif

Our Header File:

//...
class ObjectType : public ImportantStruct {
// Our extra fields
}
//...

I guess I have two questions:

1) Is this even legal?

2) What problems does this create when the C program tries to use the struct part of the object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:55:08+00:00Added an answer on June 14, 2026 at 3:55 am

    Since the ImportantStruct is a POD structure (automatically it has standard layout), and since ObjectType doesn’t have other base types and if it doesn’t have virtual methods, it also has a standard layout. Therefore it can be used as a struct in C.

    1) Is this even legal?

    Yes, it is.

    2) What problems does this create when the C program tries to use the struct part of the object?

    If your c function doesn’t try to over-write it, you are safe. You can overwrite it like this :

    void foo( struct ImportantStruct *s )
    {
     memset( s,0, sizeof(*s) );
    }
    

    If it does, then it depends on what is in ImportantStruct, and whether there are padded bytes.

    Structure like this :

    typedef struct {
      int a;
      char b;
    } ImportantStruct;
    

    might have padded bytes removed when made base class.

    Anyway, I would use composition, and avoid any kind of problems. But you have to make sure it is the first member, and that the ObjectType has standard layout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: I would like to dismiss a modalView that I have presented earlier and
Background: I've wrote a small library that is able to create asp.net controls from
Background: I have a css and a js that is used only by the
Background If a third party library has code like class ThirdPartyClass { public: int
Background: We're building an application that allows our customers to supply data in a
Background I am working with a monad built of a stack of transformers one
Background: Using unix, codeigniter from localhost. I'd like to run a controllers via a
Background The main application where I work is based heavily on the MUMPS-esque Caché
Background: My employeer at my non-programming job knows that I am an undergraduate CS
Background: I have a performance-critical query I'd like to run and I don't care

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.