开发者

How can I return multiple values in a DLL-safe way in C++?

I would like to return multiple values in a cross DLL-safe way in C++.

Sho开发者_如何学运维uld I use a stuct/class ?


Although your question is not very clear however, here are a few hints:

  1. A class/struct is meant to group things. Although a struct meant for grouping all the return values can be made but it really depends on how closely related those return values are. This is more a matter of adhering to conventions and OOP principals.

  2. To return multiple values, you can use pointers and references. e.g., following function prototype returns a bool in a conventional way but accepts two pointers as parameters. The objects to which these pointers point to can be changed in the callee function and hence, multiple values can be returned.

Function Prototype:

bool returnValues(int i, char* c, int* result);


Use PIMPL idioms:

In header (dll safe surface):

class Type;

class MyClass
{
public:
  int GetNumberOfType();
  Type * GetValue(int i) { return this->values[i]; };
protected:
  Type ** values;
};

In Source:

#include <Type.h> // common header to real definition and declaration
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜