Generic implementation of a smart pointer

S

software

Hello,

I am a new bee to c++/cli, here's my question: I have a template
version of a smart pointer class I can't use in my application because
I need to use it in more than one assembly. I thought of rewriting the
class as a generic class to avoid this problem. Unfortuneatly it is
not possible to use Pointers inside generic classes.

Code:

generic< typename T> public ref class GenericSmartPtr
{
private:
T* ptr;

public:
GenericSmartPtr() : ptr(nullptr) {}
GenericSmartPtr(T* t) {ptr = t;}
!GenericSmartPtr() {delete ptr;}
~GenericSmartPtr() {this->!GenericSmartPtr();}
}

this compiles with Error C3229
If I'd used template instead of generic this would compile, but cannot
be used outside the assembly, which is necessary.

Please help me your ideas!
Jens
 
B

Ben Voigt [C++ MVP]

Hello,

I am a new bee to c++/cli, here's my question: I have a template
version of a smart pointer class I can't use in my application because
I need to use it in more than one assembly. I thought of rewriting the
class as a generic class to avoid this problem. Unfortuneatly it is
not possible to use Pointers inside generic classes.

Code:

generic< typename T> public ref class GenericSmartPtr
{
private:
T* ptr;

public:
GenericSmartPtr() : ptr(nullptr) {}
GenericSmartPtr(T* t) {ptr = t;}
!GenericSmartPtr() {delete ptr;}
~GenericSmartPtr() {this->!GenericSmartPtr();}
}

this compiles with Error C3229
If I'd used template instead of generic this would compile, but cannot
be used outside the assembly, which is necessary.

Please help me your ideas!

I think the best you can do is:

template< typename T> public ref class GenericSmartPtr { ... };

public ref class BotPtr : GenericSmartPtr<Bot> { ... };
public ref class SystemPtr : GenericSmartPtr<System> { ... };

in one assembly, and use the exported non-template types in every other
assembly.
 
S

software

public ref class BotPtr : GenericSmartPtr said:
public ref class SystemPtr : GenericSmartPtr<System> { ... };

in one assembly, and use the exported non-template types in every other
assembly.

Hello Ben,

thanks for the hint. It works!

Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top