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

  • Home
  • SEARCH
  • 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 8274833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:52:18+00:00 2026-06-08T07:52:18+00:00

I’m looking at wrapping some c libraries in c++ and I’m not sure what

  • 0

I’m looking at wrapping some c libraries in c++ and I’m not sure what is the best way to wrap opaque pointers.

When the C-structure is part of the public API

typedef struct _SomeType
{
    int a;
    int b;
} SomeType_t;

Where there are several “member” functions:

void SomeTypeFoo( SomeType_t* obj, ... );
void SomeTypeBar( SomeType_t* obj, ... );

I like the approach of deriving from the base to simply associate these “member” functions as actual class members. i.e.:

class SomeTypeWrapper:
    public SomeType_t
{
    void foo( ... );
    void bar( ... );
};

As far as my understanding goes, I believe that SomeTypeWrapper is “binary compatable” with struct _SomeType such that SomeTypeWrapper* can be casted toSomeType_t* so that the c++ method implementation might be:

SomeTypeWrapper::foo( ... )
{
    SomeTypeFoo( (SomeType_t*)this, ... );
}

See Note[1].

However, in some libraries they like to keep the definition of the structure private (I suppose this is to allow changes to the implementation without changing the headers/API ). So in the headers I have something like this:

typedef struct _SomeType SomeType_t;

And then all of the “member” functions deal with these opaque pointers. In order to “wrap” these methods into an interface, what I’ve done so far is to provide a class which contains the pointer.

class SomeTypeWrapper
{
    private:
        SomeType_t* m_data;

    public:
        SomeTypeWrapper( SomeType_t* data ): m_data(data){}
        void foo(...);
        void bar(...);
};

This means that for any function in the c library which returns such an opaque pointer, I have to allocate a new object to carry that pointer around. For a lot of libraries that is probably fine but this design complicates implementing the wrapper and for speedy c-libraries that do a lot of small-object allocation, I suspect that this extra object over-head can lead to a significant performance penalty. In addition, the underlying object may be reference counted so in some cases I have to make decisions about whether SomeWrapperType increases the reference count, implements a copy constructor, has a private constructor, etc.

In most cases I’m happy to pay that penalty for the nicer interface, but I’m looking for other options. Is there a cleaner way of dealing with opaque c-pointers in a c++ wrapper library? Related: is there a good way to implement wrapper classes so that the c++ headers don’t have to include the c-library headers AND doesn’t require wrapper object allocation?

Note[1]: I believe this cast is safe as long as the same compiler is used and SomeTypeWrapper adds no additional members and has no virtual methods. By same compiler, I’m assuming the c-library compiled by gcc will work with the c++ wrapper compiled by the same gcc. Perhaps that is not guaranteed ?

  • 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-08T07:52:19+00:00Added an answer on June 8, 2026 at 7:52 am

    Your final version of SomeTypeWrapper (in the question) is the way to go forward.

    This means that for any function in the c library which returns such an opaque pointer, I have to allocate a new object to carry that pointer around. For a lot of libraries that is probably fine but this design complicates implementing the wrapper and for speedy c-libraries that do a lot of small-object allocation, I suspect that this extra object over-head can lead to a significant performance penalty.

    There is no overhead. An instance of a class with one data member and no virtual methods is no bigger than its sole member. If you define the methods inline in the header, there’ll be no function call overhead either. So, as long as you don’t allocate these instances with new, but on the stack:

    SomeTypeWrapper some_object(make_some_object());  // one allocation on the heap
    

    then you have a zero-overhead solution.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I need a function that will clean a strings' special characters. I do NOT
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.