类在C ++ DLL

时间:2018-01-16 10:08:24

标签: c++ visual-c++ dll

要在DLL中使用类,为了兼容编译器,使用https://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL#CppMatureApproach中的方法。这要求类从抽象类派生为"接口"并覆盖抽象类的功能。如果DLL的类具有返回该类值的函数,那么如何派生它并覆盖抽象类的函数,因为函数必须具有相同的返回值并且不能返回抽象类的值。除了制作第三个未导出的类,它可以很容易地转换为将导出而没有数据丢失的原始类,如何以与其他编译器一起使用的方式从DLL导出类?

原始代码:仅适用于相同的编译器。

#pragma once

#ifdef UNSIGNEDBIGINT_EXPORTS
#define UNSIGNEDBIGINT_API __declspec(dllexport)
#else
#define UNSIGNEDBIGINT_API __declspec(dllimport)
#endif

#include "stdafx.h"

typedef std::deque<short int> list;

//Unsigned Bigint class.



class UNSIGNEDBIGINT_API Unsigned_Bigint
{
private:
    list digits;
    Unsigned_Bigint removeIrreleventZeros(Unsigned_Bigint);

    //Arithmetic Operations
    Unsigned_Bigint add(Unsigned_Bigint);
    Unsigned_Bigint subtract(Unsigned_Bigint);
    Unsigned_Bigint multiply(Unsigned_Bigint);
    Unsigned_Bigint divide(Unsigned_Bigint, bool);

    //Comparison Operations
    bool greater(Unsigned_Bigint);

public:
    Unsigned_Bigint();
    Unsigned_Bigint(int);
    Unsigned_Bigint(list);
    Unsigned_Bigint(const Unsigned_Bigint&);
    ~Unsigned_Bigint();
    inline list getDigits() const;

    //Overloaded Arithmetic Operators
    inline Unsigned_Bigint operator+(Unsigned_Bigint);
    inline Unsigned_Bigint operator-(Unsigned_Bigint);
    inline Unsigned_Bigint operator*(Unsigned_Bigint);
    inline Unsigned_Bigint operator/(Unsigned_Bigint);
    inline Unsigned_Bigint operator%(Unsigned_Bigint);

    //Overloaded Comparison Operators
    inline bool operator==(Unsigned_Bigint);
    inline bool operator!=(Unsigned_Bigint);
    inline bool operator>(Unsigned_Bigint);
    inline bool operator<(Unsigned_Bigint);
    inline bool operator>=(Unsigned_Bigint);
    inline bool operator<=(Unsigned_Bigint);

    //Overloaded Asignment Operators
    inline void operator=(Unsigned_Bigint);
    inline void operator+=(Unsigned_Bigint);
    inline void operator-=(Unsigned_Bigint);
    inline void operator*=(Unsigned_Bigint);
    inline void operator/=(Unsigned_Bigint);
    inline void operator%=(Unsigned_Bigint);

    //Increment/Decrement
    inline Unsigned_Bigint& operator++();
    inline Unsigned_Bigint& operator--();
    inline Unsigned_Bigint operator++(int);
    inline Unsigned_Bigint operator--(int);

    //Exponent
    Unsigned_Bigint exponent(Unsigned_Bigint);

};

//Ostream
UNSIGNEDBIGINT_API inline std::ostream& operator<<(std::ostream&, Unsigned_Bigint);

编辑:还存在抽象基类将自身作为参数的问题,这是非法的,因为它是一个抽象类。使用引用需要在代码中进行大量修改。还有其他选择吗?

2 个答案:

答案 0 :(得分:1)

  

因为函数必须具有相同的返回值,并且不能返回抽象类的值

不是,C ++允许所谓的covariant return types,这意味着var target = $('.parallax').attr('id'), $window = $(window); $window.on('load resize scroll',function(){ var $div = $('#parallax'+target); if ( $window.scrollTop() >= $div.offset().top ) { $div.addClass('link'); } else { $div.removeClass('link'); } }); 方法的子类可以用子类型替换原始返回类型。但即使您没有,也可以将子类的实例作为父类返回。但是,您可能遇到的问题是slicing。如果要返回值(而不是引用或指针),则在使用父类接口时,子类中的其他成员将丢失。您可以使用额外的间接(例如带指针的结构)或其他策略来避免它。

答案 1 :(得分:0)

正如你所链接的文章所说明了这种方法的缺点:

  

抽象接口方法不能返回或接受常规C ++   对象作为参数。它可以是内置类型(如int,   double,char *等)或其他抽象接口。这是相同的   COM接口的限制。

那么可能返回一个指向类实现的asbtract接口的指针?