POV-Ray : Newsgroups : povray.off-topic : Learning C++ : Re: Learning C++ Server Time
6 Sep 2024 21:19:37 EDT (-0400)
  Re: Learning C++  
From: Nicolas Alvarez
Date: 13 Dec 2008 11:39:17
Message: <4943e534@news.povray.org>
Warp wrote:
> Nicolas Alvarez <nic### [at] gmailcom> wrote:
>> A good approach (that few people use) is making the library in C++ and
>> making a C wrapper :)
> 
>   That would be rather difficult because it's not possible to call C++
> functions from C. (The C compiler cannot understand C++ name mangling,
> and obviously none of the features C++ has that C doesn't.)

Isn't this possible?

cwrap.h:

struct Foo_Obj; //fwd declaration

#ifdef __cplusplus
extern "C" {
#endif

struct Foo_Obj* foo_obj_create();
void foo_obj_dosomething(Foo_Obj* obj);
void foo_obj_delete(Foo_Obj* obj);

#ifdef __cplusplus
}
#endif

cwrap.cpp:

#include "foo.hpp"

struct Foo_Obj {
    FooLib::Obj realthing;
};

extern "C" {

struct Foo_Obj* foo_obj_create() {
    return new Foo_Obj;
}
void foo_obj_delete(struct Foo_Obj* obj) {
    delete obj;
}
void foo_obj_dosomething(struct Foo_Obj* obj) {
    obj->realthing.dosomething();
}

} //extern "C"


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.