`
baiguomeng
  • 浏览: 948899 次
文章分类
社区版块
存档分类
最新评论

如何将导出的标准模板库 (STL) 类和包含数据成员是 STL 对象的类的实例化

 
阅读更多
本文讨论了如何执行以下任务:
  • 导出实例化的一个标准模板库 (STL) 类。
  • 导出包含一个 STL 的一个数据成员的类对象。
请注意您可以不导出一个通用的模板。必须进行实例化模板 ; 也就是所有模板参数时必须提供,并且必须完全定义的类型的实例化。 实例"堆栈 <int>;"实例化 STL 堆栈类。在实例化强制类堆栈 <int>来生成的所有的成员。

此外请注意一些 STL 容器 (图、 组、 队列、 列表、 deque) 不能被导出。请参阅更多信息一节进行详细说明。

   // -------------------------------------------
   // 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 <vector>

    // 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<int> and vector<char>
    // This does not create an object. It only forces the generation of all
    // of the members of classes vector<int> and vector<char>. It exports
    // them from the DLL and imports them into the .exe file.
    EXPIMP_TEMPLATE template class DECLSPECIFIER std::vector<int>;
    EXPIMP_TEMPLATE template class DECLSPECIFIER std::vector<char>;

    // 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<int> VectorOfInts;
        static std::vector<char> 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<MyClass>
    // This does not create an object. It only forces the generation of
    // all of the members of the class vector<MyClass>. It exports them
    // from the DLL and imports them into the .exe file.
    EXPIMP_TEMPLATE template class DECLSPECIFIER std::vector<MyClass>;

    // -------------------------------------------
    // Compile options needed: /GX /LDd /MDd /D"EXP_STL"
    //                     or: /GX /LD  /MD  /D"EXP_STL"
    // DLL.CPP

    #include "MyHeader.h"
    std::vector<char> MyClass::StaticVectorOfChars;

    // -------------------------------------------
    // Compile options needed: /GX /MDd
    //                     or: /GX /MD
    // EXE.CPP

    #include <iostream>
    #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<int>::iterator vii = x.VectorOfInts.begin();
        while (vii != x.VectorOfInts.end())
        {
            std::cout << *vii;
            std::cout << " displayed from x.VectorOfInts" << std::endl;
            vii++;
        }
        std::vector<char>::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<MyClass> vy;
        for (i=0; i=5; i++) vy.push_back(MyClass());

        return 1;
    }
分享到:
评论

相关推荐

    C++标准程序库STL的架构

    5 STL标准程序库 16 5.1 STL组件 16 5.1.1 分类 16 5.1.2 基本观念 16 5.1.3 好处 16 5.2 容器(containers) 16 5.2.1 分类 16 5.2.2 序列式容器示例 16 5.2.3 关联式容器 18 5.3 迭代器 18 5.3.1 示例 19 5.3.2 ...

    C++标准程序库STL

    更明确地说,本书将焦点放在标准模板库身上,检验其中的容器、迭代器、仿函数和算法。你还可以找到特殊容、字串、数值类别、国际化议题、IOStream。每一个元素都有深刻的呈现,包括其介绍、设计、运用实例、细部解说...

    STL库string类的模拟实现

    4. string类是basic_string模板类的一个实例,它使用char来实例化basic_string模板类,并用char_traits 和allocator作为basic_string的默认参数(根于更多的模板信息请参考basic_string)。 5. 注意,这个类独立于所...

    Bitmatrix-CPP:从STL-位集类扩展的模板化位-matrix类

    它由包含位集的模板化STL数组组成,以创建2D位矩阵类。 Bitmatrix是在编译时推导的模板类型,一旦创建了bitmatrix,便无法更改其大小 1)如果要使用Visual Studio 2019,则需要使用Visual Studio 2019。 2)否则,...

    C++函数模板与类模板实例解析

    泛型编程最初诞生于C++中,目的是为了实现C++的STL(标准模板库)。 模板(template)是泛型编程的基础,一个模板就是一个创建类或函数的蓝图或公式。例如,当使用一个vector这样的泛型类型或者find这样的泛型函数时...

    [程序员成长课堂:C.标准教程].陈国建等.扫描版.pdf

    第6章 标准模板库STL 130 第三篇 C++面向对象编程 第7章 类 148 第8章 类的初始化、赋值和析构 169 第9章 模板 190 第10章 面向对象程序设计 213 第四篇 C++编程实践 第11章 教学管理系统的C++实现 260 附录A Visual...

    谭浩强C语言程序设计,C++程序设计,严蔚敏数据结构,高一凡数据结构算法分析与实现.rar

    8.2 类的声明和对象的定义 8.2.1 类和对象的关系 8.2.2 声明类类型 8.2.3 定义对象的方法 8.2.4 类和结构体类型的异同 8.3 类的成员函数 8.3.1 成员函数的性质 8.3.2 在类外定义成员函数 8.3.3 inline成员函数 8.3.4...

    (中文版)C++标准程序库

    更明确地说,这本书将焦点放在标准模板库身上,检验其中的容器、迭代器、仿函数和算法。读者还可以找到特殊容、字串、数值类别、国际化议题、IOStream。每一个元素都有深刻的呈现,包括其介绍、设计、运用实例、细部...

    谭浩强C语言程序设计,C++程序设计,严蔚敏数据结构,高一凡数据结构算法分析与实现.rar )

    8.2 类的声明和对象的定义 8.2.1 类和对象的关系 8.2.2 声明类类型 8.2.3 定义对象的方法 8.2.4 类和结构体类型的异同 8.3 类的成员函数 8.3.1 成员函数的性质 8.3.2 在类外定义成员函数 8.3.3 inline成员函数 8.3.4...

    C++标准程序库

    更明确地说,这本书将焦点放在标准模板库身上,检验其中的容器、迭代器、仿函数和算法。读者还可以找到特殊容、字串、数值类别、国际化议题、IOStream。每一个元素都有深刻的呈现,包括其介绍、设计、运用实例、细部...

    C++大学教程,一本适合初学者的入门教材(part2)

    11.2.2 输入/输出流类和对象 11.3 输出流 11.3.1 流插入运算符 11.3. 2 连续使用流插入/流读取运算符 11.3.3 输出char类型的变量 11.3.4 用成员函数put输出字符和put函数的连续调用 11.4 输入流 ...

    C++大学教程,一本适合初学者的入门教材(part1)

    11.2.2 输入/输出流类和对象 11.3 输出流 11.3.1 流插入运算符 11.3. 2 连续使用流插入/流读取运算符 11.3.3 输出char类型的变量 11.3.4 用成员函数put输出字符和put函数的连续调用 11.4 输入流 ...

    C++STL程序员开发指南【可搜索+可编辑】

    2-1 C扫标准库简介...................................................... 44 2-1-1 1/0 流技术..................................................... 46 2-1-2 string 类........................................

    -C++参考大全(第四版) (2010 年度畅销榜

    第四部分讨论了标准类库,包括STL(标准模板库);第五部分显示了两个应用C++和面向对象编程的实际例子。 本书内容全面、翔实,是学习C++编程语言的广大学生的一部有用的工具书,也是对C++感兴趣的读者的必备参考书。 ...

    C++标准程序库(简体)part1

    更明确地说,本书将焦点放在标准模板库(Standard Template Library)身上,检验其中的容器(containers)、迭代器(iterators)、仿函数(functors)和算法(algorithms)。你还可以找到特殊容器、字符串(strings...

    C++标准程序库(简体)part2

    更明确地说,本书将焦点放在标准模板库(Standard Template Library)身上,检验其中的容器(containers)、迭代器(iterators)、仿函数(functors)和算法(algorithms)。你还可以找到特殊容器、字符串(strings...

    Visual C++ 2010入门经典(第5版)--源代码及课后练习答案

    7.2 数据类型、对象、类和实例 301 7.2.1 类的起源 303 7.2.2 类的操作 303 7.2.3 术语 303 7.3 理解类 304 7.3.1 定义类 304 7.3.2 声明类的对象 305 7.3.3 访问类的数据成员 305 7.3.4 类的成员函数 307 ...

Global site tag (gtag.js) - Google Analytics