创建没有静态方法的c ++ DLL [英] Creating c++ DLL without static methods

查看:90
本文介绍了创建没有静态方法的c ++ DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中创建一个DLL。下面是一个例子:

I am creating a DLL in C++. Here is an example:

namespace MathFuncs
{
  class MyMathFuncs
  {
    public:
        // Returns a + b
        static __declspec(dllexport) double Add(double a, double b);

        // Returns a - b
        static __declspec(dllexport) double Subtract(double a, double b);

        // Returns a * b
        static __declspec(dllexport) double Multiply(double a, double b);

        // Returns a / b
        // Throws DivideByZeroException if b is 0
        static __declspec(dllexport) double Divide(double a, double b);
    };
}

所有方法都是静态的,静态方法有很多限制,所以我的问题是如何实现相同没有静态方法?我总是需要在DLL中有静态方法?我想在C#和IOS应用程序中导入此DLL。

All methods are static and there are lot of limitations with static methods so my question is how can I implement the same without static methods? Do I always need to have static methods in DLL? I want to import this DLL in C# and IOS apps.

推荐答案

作为旁注我几天前用mingw /

As side note I did 1 experiment few days ago with mingw/c++ wich can be clarifying for you.

我有一个全局引用计数器,用于在我的程序中找出内存泄漏,

I had a Global reference counter for find out memory leaks in my program,

class ReferenceCounter /** other implementations details are omitted.*/
{
public:

static int GlobalReferenceCounter;

//version 1
static int getReferenceCount1() { return GlobalReferenceCounter;}

//verison 2
static int getReferenceCount2(); //same code of version 1 but moved into .cpp file
};

当使用引用计数器将我的库编译到DLL中时,变量被复制,1个版本被编译进入DLL,一个版本在客户端代码中编译。

When compiling my library using reference counter into a DLL, then the variable is duplicated, 1 version is compiled into the DLL, and one version is compiled in client code.

当我问从引用计数的东西从DLL的工厂方法,只有引用计数器DLL增加/减少。当客户端代码使用继承自Ref Counter的自己的类时,则客户端引用计数器增加/减少。

When I ask istances of reference counted stuff from the factory methods of the DLL, only the reference counter inside the DLL is increased/reduced. When client code is using its own classes inherited from Ref Counter, then the client reference counter is increased/reduced.

因此,为了检查内存泄漏,我必须在结束时程序

So for check for memory leaks I must do at end of the program

assert(ReferenceCounter.getReferenceCount1() == 0);
assert(ReferenceCoutner.getReferenceCount2() == 0);

这是因为在内存泄漏的情况下,这些值之一将大于0.如果第一个值更大那么内存泄漏是由未分配的用户类引起的,如果第二个值大于0,那么内存泄漏是由库类引起的。

that's because in case of memory leak one of those values will be greater than 0. If first value greater than 1, then the memory leak is caused by unallocated user classes, if second value is greater than 0, then memory leak is caused by library classes.

是由未分配的库的类引起的,这不一定是库的一个错误,因为用户仍然能够泄漏该类,即使这意味着在库中缺乏设计,因为理想情况下,everythin应该返回正确的智能指针)

Note that if leak is caused by unallocated library's classes, this is not necessarily a bug of the library, since user is still able to leak that classes, even if that should mean a lack of design in the library, since ideally everythin should be returned in proper smart pointers for safety.)

当然你应该指定GlobalReferenceCoutner在文档中重复,否则一些不知道的用户可以认为2个getter是多余的,并会认为你做了一些错误。 (如果可能避免做这样的事情,是晦涩和不清楚的)

Of course you should specify that "GlobalReferenceCoutner" is duplicated in the documentation, else some unaware user can just think that 2 getters are redundant and will think you did some mistake. (if possible avoid to do something like that, is obscure and unclear)

这也应该警告,通过DLL访问静态方法是非常不安全的。例如如果在我的类中,我只想有一个引用计数器而不是2我必须这样做为授予安全:

That should also warn you that accessing static method through DLLs is highly unsafe. For example If In my class I wanted to have only 1 reference counter instead of 2 I had to do that for grant safety:

class ReferenceCounter
{
public:

static int GlobalReferenceCounter;

static void duplicate() { increaseGlobalCounter(); }

static void release() { decreaseGlobalCounter(); }

static int getGlobalCounter() { return privateGetGlobalCounter(); }

private:

static increaseGlobalCounter(); // implementation into Cpp file

static decreaseGlobalCounter(); // implementation into Cpp file

static privateGetGlobalCounter(); // implementation into Cpp file

};

这样做会授予您在DLL bounduaries和用户应用程序中使用相同的值。所以不是有两个不同的变量在这里我们使用1变量(可能GlobalCounter仍然编译成用户可执行文件,但没有人正在使用它删除克隆效果)

doing that will grant you that the same value is used across DLL bounduaries and in User application. so instead of have 2 different variables here we use 1 variable (probably the GlobalCounter is still compiled into user executable, but no one is using it removing the "clone effect")

这篇关于创建没有静态方法的c ++ DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆