Archived

This topic is now archived and is closed to further replies.

krumms

Casting Smart Pointers

Recommended Posts

Hey all, Are there any well-known methods for casting between smart pointers of different types? e.g.:

class A
{
};

class B : public A
{
}

// exactly how you''d declare a smart pointer in my engine:

#define QUILL_DYNAMIC_PTR( x ) Quill::QuillPtr >

QUILL_DYNAMIC_PTR( A ) PtrToA = new A;
QUILL_DYNAMIC_PTR( B ) PtrToB = new B;

// this is impossible, due to C++ type technicalities
QUILL_DYNAMIC_PTR( A ) CastedB = PtrToB;

 
So far as I can understand, the only way to do such a thing would be to reimplement my smart pointer class to deal with such cases (at the moment my smart pointer class (QuillPtr) remains the same, whether dealing with new/delete allocated memory or COM pointers - only the RefCounter used by the QuillPtr ever needs to be reimplemented). Does anyone have any ideas? I''ll provide more source code as needed Tom

Share this post


Link to post
Share on other sites
Create a new smart pointer, copy the raw pointer, destroy the old smart pointer.

Have a look at Boost''s smart pointers, or Loki''s (if you like template code).

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]

Share this post


Link to post
Share on other sites
Thanks very much, but wouldn''t that screw up reference counts for the original pointer? Maybe I''m misunderstanding. Anyway, I''ll check out the boost/Loki source code.

Thanks again

Tom

Share this post


Link to post
Share on other sites