Boost logo

Boost Users :

Subject: Re: [Boost-users] Using C++ class with namespace in C
From: Juan Ramírez (juanantonio.ram_at_[hidden])
Date: 2013-04-25 18:01:29


I ussualy do something as the following:

// apple.h
typedef void *APPLE_HANDLE;

APPLE_HANDLE pick_an_apple();
void eat_an_apple(APPLE_HANDLE hApple, int bites);

// apple.cpp

APPLE_HANDLE pick_an_apple() {
  return new CApple();
}

void eat_an_apple(APPLE_HANDLE hApple, int bites) {
  CApple *apple = reinterpret_cast<CApple*>(hApple);
  apple->eat(bites);
}

Note that stuff like 'extern "C"' was ommited in order to make it cleaner.

__________
Saludos!
     Juan

On Thu, Apr 25, 2013 at 1:11 AM, Richard Damon <Richard_at_[hidden]>wrote:

> On 4/24/13 1:57 PM, Somostetoi Kilato wrote:
>
> Hi Richard,
>
> Thank you for your comment. You are totally right, but this was only a
> small example what I made after a long learning period :-)
> But I would like to know, if I have for example a small C++ library,
> within the classes are in a custom name space, how should I write my C
> wrapper to use those classes?
>
> Thank you and all the best,
> Kilato Somostetoi
>
>
> C code can not directly reference a class/struct that uses any feature
> not in C, this includes things like namespaces, access specifiers, member
> functions, etc. You may be able to get some of these to compile with #ifdef
> __cplusplus conditionals.
>
> If the C code only needs to deal with pointers to the class, then you can
> either use the void* pointers as you example (and the wrapper classes then
> need to use void* pointers too), or I believe you can use a pointer to an
> incomplete struct.
>
> Thus the C code could see things like
>
> struct Apple;
>
> extern Apple* pick_an_apple();
> ...
>
> Apple* the_apple = pick_an_apple();
>
>
> while the C++ code could be someing like:
>
> namespace custom {
>
> class Apple {
> ...
> };
> };
>
> extern "C" custom::Apple* pick_an_apple() {
> }
>
> This should work as C code does NOT use the actual name of a struct to
> determine if structs are compatible, and all struct pointers are stored the
> same way, so you can cast one struct pointer to another type of struct
> pointer and back and be allowed to access the object.
>
> It may be easiest to write the code with something like:
>
> #ifdef __cplusplus
> namespace custom {
> call Apple {
>
> };
> };
> typedef custom::Apple *pApple;
>
> #else
> struct Apple;
> typedef Apple *pApple;
> #endif
>
> now pApple will be a pointer to a incomplete struct in C, suitable for
> passing around addresses, while in C++ code, will be a pointer to the class
> in its namespace. Of course, any function that the C code calls must be
> declared extern "C".
>
> --
> Richard Damon
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net