博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DLL导出STL模板类和成员变量
阅读量:4223 次
发布时间:2019-05-26

本文共 3858 字,大约阅读时间需要 12 分钟。

// -------------------------------------------   // MYHEADER.H   //disable warnings on 255 char debug symbols    #pragma warning (disable : 4786)   //disable warnings on extern before template instantiation    #pragma warning (disable : 4231)    #include 
    // Provide the storage class specifier (extern for an .exe file, null    // for DLL) and the __declspec specifier (dllimport for .an .exe file,    // dllexport for DLL).    // You must define EXP_STL when compiling the DLL.    // You can now use this header file in both the .exe file and DLL - a    // much safer means of using common declarations than two different    // header files.    #ifdef EXP_STL    #    define DECLSPECIFIER __declspec(dllexport)    #    define EXPIMP_TEMPLATE    #else    #    define DECLSPECIFIER __declspec(dllimport)    #    define EXPIMP_TEMPLATE extern    #endif    // Instantiate classes vector
 and vector
    // This does not create an object. It only forces the generation of all    // of the members of classes vector
 and vector
. It exports    // them from the DLL and imports them into the .exe file.    EXPIMP_TEMPLATE template class DECLSPECIFIER std::vector
;    EXPIMP_TEMPLATE template class DECLSPECIFIER std::vector
;    // Declare/Define a class that contains both a static and non-static    // data member of an STL object.    // Note that the two template instantiations above are required for    // the data members to be accessible. If the instantiations above are    // omitted, you may experience an access violation.    // Note that since you are exporting a vector of MyClass, you must    // provide implementations for the operator < and the operator ==.    class DECLSPECIFIER MyClass    {    public:        std::vector
 VectorOfInts;        static std::vector
 StaticVectorOfChars;    public:        bool operator < (const MyClass > c) const        {            return VectorOfInts < c. VectorOfInts;        }        bool operator == (const MyClass > c) const        {            return VectorOfInts == c. VectorOfInts;        }    };    // Instantiate the class vector
    // This does not create an object. It only forces the generation of    // all of the members of the class vector
. It exports them    // from the DLL and imports them into the .exe file.    EXPIMP_TEMPLATE template class DECLSPECIFIER std::vector
;
 // -------------------------------------------    // Compile options needed: /GX /LDd /MDd /D"EXP_STL"    //                     or: /GX /LD  /MD  /D"EXP_STL"    // DLL.CPP    #include "MyHeader.h"    std::vector
 MyClass::StaticVectorOfChars;
// -------------------------------------------    // Compile options needed: /GX /MDd    //                     or: /GX /MD    // EXE.CPP    #include 
    #include "MyHeader.h"    int main ()    {        MyClass x;        for (int i=0; i<5; i++) x.VectorOfInts.push_back(i);        for (char j=0; j<5; j++) x.StaticVectorOfChars.push_back('a' + j);        std::vector
::iterator vii = x.VectorOfInts.begin();        while (vii != x.VectorOfInts.end())        {            std::cout << *vii;            std::cout << " displayed from x.VectorOfInts" << std::endl;            vii++;        }        std::vector
::iterator vci = x.StaticVectorOfChars.begin();        while (vci != x.StaticVectorOfChars.end())        {            std::cout << *vci;            std::cout << " displayed from MyClass::StaticVectorOfChars";            std::cout << std::endl;            vci++;        }        std::vector
 vy;        for (i=0; i=5; i++) vy.push_back(MyClass());        return 1;    }转自:https://my.oschina.net/u/2424583/blog/524001

转载地址:http://zcemi.baihongyu.com/

你可能感兴趣的文章
mysql 随机分页的优化
查看>>
DB2快速创建测试库
查看>>
利用db2look查看ddl
查看>>
SD卡驱动分析--基于高通平台
查看>>
[图文] Seata AT 模式分布式事务源码分析
查看>>
pm 源码分析
查看>>
Sending the User to Another App
查看>>
kmsg_dump
查看>>
Getting a Result from an Activity
查看>>
Allowing Other Apps to Start Your Activity
查看>>
dev/mem
查看>>
pfn_valid 源码分析
查看>>
dev/kmem 和dev/mem的区别
查看>>
test-definitions/blob/master/auto-test/bigdata/bigdata.sh
查看>>
/test-definitions/blob/master/auto-test/blktrace/blktrace.sh
查看>>
test-definitions/blob/master/auto-test/blogbench/blogbench.sh
查看>>
test-definitions/blob/master/auto-test/boost/boost.sh
查看>>
Java多态性理解
查看>>
Intellij Idea 工具在java文件中怎么避免 import .*包,以及import包顺序的问题
查看>>
IDEA Properties中文unicode转码问题
查看>>