Skip to main content
gamedev.net
🔒 Locked

Casting Smart Pointers

Started by krumms Aug 14, 2002 at 12:51 AM 1 replies 500+ views
Original Post
krumms
krumms
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
refrain_from_stupidity( &me );
Shannon Barber
Shannon Barber
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)]
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
krumms
krumms
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
refrain_from_stupidity( &me );

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.