如何创建一个C功能的新对象,并传递一个参考呢? [英] How to create a new object in a c function and pass a reference to it?
问题描述
我的问题的本质是这样的:
什么是在c函数调用者创建一个新对象,并通过一些参考的新对象,最好的办法?
What is the best approach for creating a new object in a c function and pass some reference to that new object to the caller?
我的整体环境是一个比较复杂的,涉及混合C和C ++ code,所以我在内的一些背景资料:
我创建一个C ++库的3D引擎。
库旨在被从不同的平台上的主机的应用中。
I am creating a c++ library for a 3D engine. The library is intended to be used from host applications on different platforms.
为了方便兼容性原因我现在有意隐藏纯C的API背后所有的C ++ code。
此工程pretty好为止。
For easy compatibility reasons I now intent to hide all c++ code behind an API in plain c. This works pretty well so far.
我不知道的是,唯一的事情,这是最好的办法实际上创建的通过我的C API我的引擎实例。
The only thing I am not sure about is, which is the best way for actually creating my engine instance through my c API.
我的第一种方法是这样的:
My first approach was this:
// in myEngineAPI.h
void myEngineCreate(void * myEngine);
void myEngineRelease(void * myEngine);
// in myEngineAPI.cpp
#include "myEngineAPI.h"
#include "myPrivateCppEngine.h"
void myEngineCreate(void * myEngine) {
myEngine = new Engine; // <- this doesn't seem to work as expected
}
void myEngineRelease(void * myEngine) {
delete ((Engine *)myEngine);
}
// in my host application
#include "myEngineAPI.h"
void * myEngine = NULL;
myEngineCreate(myEngine); // <- problem: myEngine is still NULL after this line.
// ...draw some fancy stuff...
myEngineRelease(myEngine);
我期望 myEngineCreate(无效* myEngine)
将我的新创建的对象的地址分配给 myEngine
。但在函数返回之后, myEngine
仍然指向NULL。为什么呢?
I would expect that myEngineCreate(void * myEngine)
would assign the address of my newly created object to myEngine
. But after the function returns, myEngine
still points to NULL. Why?
现在我的第二个方法是这样的:
Now my second approach was this:
// in myEngineAPI.h
void * myEngineCreate();
void myEngineRelease(void * myEngine);
// in myEngineAPI.cpp
#include "myEngineAPI.h"
#include "myPrivateCppEngine.h"
void * myEngineCreate() {
return new Engine; // <- ok, the function returns a valid pointer
}
void myEngineRelease(void * myEngine) {
delete ((Engine *)myEngine);
}
// in my host application
#include "myEngineAPI.h"
void * myEngine = myEngineCreate(); // <- yay, I have a void pointer to my engine
// ...draw some fancy stuff...
myEngineRelease(myEngine);
这工作。 myEngineCreate()
给我一个不透明的指针,我的引擎实例,我可以在我的后续绘制调用使用,这点我也可以用手来清理内存,当我释放功能我用它做。
这种方法的问题是,我的探查抱怨在 myEngineCreate内存泄漏()
。
据我所知,在一个地方创建对象并在另一个拥有它是一个微妙的业务,我似乎错在这里做一些事情 - 但什么?
This works. myEngineCreate()
gives me an opaque pointer to my engine instance, which I can use in my subsequent drawing calls and which I can also hand to my release function that cleans up memory when I'm done with it.
The problem with this approach is, that my profiler complains about a memory leak in myEngineCreate()
.
I understand that creating an object in one place and owning it in another is a delicate business, and I seem to do something wrong here - but what?
在此先感谢您的任何建议或帮助。
Thanks in advance for any advice or help.
推荐答案
让我们把你的 myEngineCreate
功能:
void myEngineCreate(void * myEngine) {
myEngine = new Engine;
}
这不起作用,因为 myEngine
是 myEngineCreate
的功能范围内的局部变量。以参照用C你有一个指针传递给它传递一个指针的指针,并使用derefernece运营商分配给指针:
This does not work because myEngine
is a local variable within the scope of the myEngineCreate
function. To pass a pointer "by reference" in C you have to pass it as a pointer to a pointer, and use the derefernece operator to assign to the pointer:
void myEngineCreate(void ** myEngine) {
*myEngine = new Engine;
}
和您使用地址运算符称之为&安培;
指针:
And you call it by using the address-of operator &
of a pointer:
void * myEngine;
myEngineCreate(&myEngine);
这篇关于如何创建一个C功能的新对象,并传递一个参考呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!