Deriving from a STL for a DLL exported function 
Author Message
 Deriving from a STL for a DLL exported function

I am making a DLL. I want to export the class so that it is available to
other programs that link to it. The class is (for example) based on a pair
from the STL. The basic jist is you want all the methods of a pair, but
maybe add additional methods. Everything works fine if you remove the
"declspec" and do a static link. I understand why the compiler is
complaining because the template class does not export its methods.

Is there a technically correct way to do this sort of thing or is this usage
of inheriting from a template-based class considered bad form?

typedef std::pair<int, int> MyIntPair;

class _declspec(dllexport) CMyClass : public MyIntPair
{
    public:
        CMyClass (int a, int b) : MyIntPair(a,b) {}
        void SwapFirstAndSecond (void);  // Maybe want another method

Quote:
};

gives the following warning:

warning C4275: non dll-interface class 'std::pair<int,int>' used as base for
dll-interface class 'CMyClass'



Sun, 28 Sep 2003 11:46:19 GMT  
 Deriving from a STL for a DLL exported function

Quote:

>I am making a DLL. I want to export the class so that it is available to
>other programs that link to it. The class is (for example) based on a pair
>from the STL. The basic jist is you want all the methods of a pair, but
>maybe add additional methods. Everything works fine if you remove the
>"declspec" and do a static link. I understand why the compiler is
>complaining because the template class does not export its methods.

>Is there a technically correct way to do this sort of thing or is this usage
>of inheriting from a template-based class considered bad form?

>typedef std::pair<int, int> MyIntPair;

>class _declspec(dllexport) CMyClass : public MyIntPair
>{
>    public:
>        CMyClass (int a, int b) : MyIntPair(a,b) {}
>        void SwapFirstAndSecond (void);  // Maybe want another method
>};

>gives the following warning:

>warning C4275: non dll-interface class 'std::pair<int,int>' used as base for
>dll-interface class 'CMyClass'

You can work around this by explicitly instantiating MyIntPair in your
DLL and exporting it, or you could just disable C4275. Being a
template, the compiler can instantiate MyIntPair wherever it's needed,
so your code will work despite the warning, but you should be aware
that this implies code duplication in every module that instantiates
MyIntPair. As ever, if you're building C++ programs out of multiple
dynamically linked parts, you will save yourself many headaches if you
link everyone to the same CRT DLL, so that all the parts share the
same CRT state, just like a traditional statically linked program.

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows



Sun, 28 Sep 2003 12:00:53 GMT  
 Deriving from a STL for a DLL exported function

Create two functions in DLL. One to new an object and one to delete it. You
can then export the pointer to this object to the clients.

yhhuang
ICQ:91074870



Sun, 28 Sep 2003 16:34:45 GMT  
 Deriving from a STL for a DLL exported function
See KB Article Q168958 "HOWTO: Exporting STL Components Inside & Outside of
a Class"
--
With best wishes,
    Igor Tandetnik


Quote:
> I am making a DLL. I want to export the class so that it is available to
> other programs that link to it. The class is (for example) based on a pair
> from the STL. The basic jist is you want all the methods of a pair, but
> maybe add additional methods. Everything works fine if you remove the
> "declspec" and do a static link. I understand why the compiler is
> complaining because the template class does not export its methods.

> Is there a technically correct way to do this sort of thing or is this
usage
> of inheriting from a template-based class considered bad form?

> typedef std::pair<int, int> MyIntPair;

> class _declspec(dllexport) CMyClass : public MyIntPair
> {
>     public:
>         CMyClass (int a, int b) : MyIntPair(a,b) {}
>         void SwapFirstAndSecond (void);  // Maybe want another method
> };

> gives the following warning:

> warning C4275: non dll-interface class 'std::pair<int,int>' used as base
for
> dll-interface class 'CMyClass'



Sun, 28 Sep 2003 21:26:32 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Exporting class from DLL when class derives from STL

2. Exporting a C function from a DLL that exports C++ classes

3. Calling an exported function of a dll which are exported Stdcall calling convention

4. Deriving from template classes and exporting from a DLL

5. Regular DLL -VS- MFC Extension DLL and exporting functions

6. Exporting STL containers (specifically std::map) from DLL?

7. exporting STL from DLL

8. Dll exports and STL string

9. Exporting a STL descendant from a DLL

10. Problems exporting STL-based classes from a DLL

11. STL members of a class exported from a DLL

12. Export STL set from DLL

 

 
Powered by phpBB® Forum Software