问题描述:

Possible Duplicate:

C++/CLI Mixed Mode DLL Creation

I'm wrapping a C++ class using c++ cli. The header file looks like this:

pabcon.h

class PABCon {

private:

unsigned int maxIndex;

long byteSize;

public:

__declspec(dllexport) inline unsigned int GetMaxIndex() { return this->maxIndex; };

__declspec(dllexport) void invertData();

};

I'm wrapping non-inline functions this way:

pabconwrapper.h

public ref class PABConWrapper

{

private:

PABCon *pabc;

public:

PABConWrapper();

~PABConWrapper();

void invertData();

};

pabconwrapper.cpp

PABConWrapper::PABConWrapper() : pabc(new PABCon())

{

}

void PABConWrapper::invertData()

{

pabc->invertData();

}

PABConWrapper::~PABConWrapper()

{

delete pabc;

}

My questions are:

1) What is the best way to wrap the inline c++ functions?

2) What should I do with private variables defined in pabcon.h? Should I wrap them too somehow?

Thanks

P.S. I've touched C++ last time 4 years ago and coded in C# since then.

网友答案:

inline: Tagging a method inline is an instruction to the compiler to not emit a function call, but to instead take the contents of the method, and put it directly into the calling function. I believe this is only advisory, the compiler can choose to emit a function call anyway. Regardless, you don't need to do anything fancy when calling this function from C++/CLI, so you don't need to do anything fancy when wrapping it. Just wrap it the same as any other method.

private variables: If they're private variables, then unmanaged C++ code using PABCon wouldn't have access to them. The public interface of PABCon is just the public methods, so that's all you need to worry about. (If the C++ class had any public variables, then you'd wrap them by creating a property in C++/CLI.)

~PABConWrapper: In C++/CLI, ~ is not the destructor, it's the dispose method. As implemented right now, you'll have a memory leak if you forget to dispose your new class. At a minimum, switch the ~ to !, and declare the finalizer instead. Ideally, implement both ! and ~, delete the unmanaged object in both methods (with a proper null check), and add proper null checks in the other methods. This would be a good & proper implementation of IDisposable.

相关阅读:
Top