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 8787925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:01:03+00:00 2026-06-13T22:01:03+00:00

My current project is a medium-sized library that is meant to have a C

  • 0

My current project is a medium-sized library that is meant to have a C and a C++ interface at the same time. It centers around a single data type that I want to be accessible from C and C++ functions, because I want to encourage third parties to extend the library by writing functions in either language.

I know about the basics of C/C++ mixing (compare for example http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html) and have come up with the following solution:

My basic design centers around creating a struct in C with all data exposed (this is what my C programmers expect) and deriving a class from it that hides member access, hopefully leading to safer access to the struct for C++ programmers. The problem comes in with the derivation: I want to use namespaces in C++ and hide the C interface. Of course, the C struct itself cannot be hidden (without resorting to the PIMPL idiom), but that’s fine for me.

The following example code compiles and runs without apparent errors with C and C++ “client” programs. However, I’m wondering if this solution is valid or if there are better solutions.

Example code:

#ifdef __cplusplus__
extern "C" {
#endif

struct base
{
    char * data;
}

#ifdef __cplusplus__
} // extern "C"
namespace {
extern "C" {
#endif

/* cleanly initialize struct */
struct base * new_base (struct base *);

/* cleanly destroy struct */
void del_base (struct base *);

#ifdef __cplusplus__
} } // namespace, extern "C"

#include<new>

namespace safe {

class base_plus : private base
{
public:
    base_plus () 
    { 
        if (! new_base(this)) 
            throw std::bad_alloc ();
    }

    ~base_plus ()
    {
        del_base (this);
    }
};

} // namespace safe

#endif
  • 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-13T22:01:04+00:00Added an answer on June 13, 2026 at 10:01 pm

    Actually, another way would be to write the full code in C++ and only write a C slim interface over this, using data hiding technics.

    namespace Foo {
        class Bar {
        public:
            int property1() const;
            std::string const& property2() const;
        };
    }
    

    And in a C-compatible header:

    #ifdef __cplusplus__
    extern "C" {
    #endif
    
    typedef void* Bar;
    
    Bar foo_bar_new(int i, char const* s);
    
    void foo_bar_delete(Bar b);
    
    int foo_bar_property1(Bar b);
    
    char const& foo_bar_property2(Bar b);
    
    #ifdef __cplusplus__
    }
    #endif
    

    With the accompanying implementation:

    Bar foo_bar_new(int i, char const* s) {
        return new Foo::Bar(i, s);
    }
    
    void foo_bar_delete(Bar b) {
        delete static_cast<Foo::Bar*>(b);
    }
    
    int foo_bar_property1(Bar b) {
        return static_cast<Foo::Bar*>(b)->property1();
    }
    
    char const* foo_bar_property2(Bar b) {
        return static_cast<Foo::Bar*>(b)->property2().c_str();
    }
    

    The two main advantages are:

    • Full-blown C++ code, with fully encapsulated data and all the goodness of a stronger type-system
    • Binary stability across releases made easier in the C interface

    Note: this is how Clang and LLVM deal with C compatibility, for example.

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

Sidebar

Related Questions

currently I have a medium sized MVC project with multiple controllers that support paging
We have a medium sized .js file that we include in our web framework
On my current project, which is a delivery system, I have a list of
In my current project, I am writing code generator. The interface would be Command
For my current project I have to send a signature from PHP to Java
my current project is based on Entity Framwork code-first. I have three types: Task,
This current project I've been assigned uses the Version 3.1 levels of: Microsoft.Practices.EnterpriseLibrary.Common; Microsoft.Practices.EnterpriseLibrary.Data;
My current project is split into multiple classes that correctly represent the way we
On my current project we're using Spring 3 MVC and have a requirement to
My current project has a .js file with hard coded data in it. So

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.