Using C++ class with namespace in C

Hi,
maybe it is not the best place to ask but at this moment I have no other
idea where to go. This question is just a preamble for all the comming,
BOOST related, questions of mine.
So, I have a C++ class, the Apple, at the moment in the std namespace:
class Apple {
public:
Apple();
void eat_an_apple(int p_bites);
private:
int bites; // number of bites to eat an apple
};
I have a C application, an apple picker, first the header:
typedef void CApple;
#ifdef __cplusplus
extern "C" {
#endif
CApple* pick_an_apple();
void eat_an_apple(const CApple *p_the_apple, int p_bites);
void wash_your_hands(CApple *p_the_apple);
#ifdef __cplusplus
} // extern "C"
#endif
and the code, too:
#include

On 4/16/13 5:04 AM, Somostetoi Kilato wrote:
Hi,
maybe it is not the best place to ask but at this moment I have no other idea where to go. This question is just a preamble for all the comming, BOOST related, questions of mine.
So, I have a C++ class, the Apple, at the moment in the std namespace: ... My question is, if I want to move my Apple class in a custom namespace, garden, how must I modify my C header and code file?
Thank you and all the best, Kilato Somostetoi
Since your C file only deals with the structure via pointers to void, moving the class into a name space will have no effect. Note, that the functions you are declaring to return/take pointers to void, really do return/take pointers to void, and the functions do the casting to Apple* inside. The thing to note is that by definition, a pointer to ANY type/object can be converted to a pointer to void and back to its original type and it will point to the same object. -- Richard Damon

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
2013/4/24 Richard Damon
On 4/16/13 5:04 AM, Somostetoi Kilato wrote:
Hi,
maybe it is not the best place to ask but at this moment I have no other idea where to go. This question is just a preamble for all the comming, BOOST related, questions of mine.
So, I have a C++ class, the Apple, at the moment in the std namespace: ... My question is, if I want to move my Apple class in a custom namespace, garden, how must I modify my C header and code file?
Thank you and all the best, Kilato Somostetoi
Since your C file only deals with the structure via pointers to void, moving the class into a name space will have no effect.
Note, that the functions you are declaring to return/take pointers to void, really do return/take pointers to void, and the functions do the casting to Apple* inside.
The thing to note is that by definition, a pointer to ANY type/object can be converted to a pointer to void and back to its original type and it will point to the same object.
-- Richard Damon
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

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

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
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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Juan Ramírez
-
Richard Damon
-
Somostetoi Kilato