Jump to content
  • Advertisement
Sign in to follow this  
ubersheep

Implicit Pointer Courier Casting

This topic is 2449 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I'm having a play with pointer couriers. However they don't appear to like implicit casting between functions. Here is the jist:


public class IGNode;
public class IGActor : public IGNode;

public func( IGPtr<IGNode> pArg );

IGPtr<IGActor> pMyActor;

func( pMyActor ); // << this produces an error!


Here's my entire IGPtr class - any ideas?


template<typename T>
class IGPtr
{
public:
IGPtr( T* pObj = NULL ) : m_pObj( pObj ) {}
~IGPtr() {}

public:
bool IsSet() const { return m_pObj != NULL; }
bool IsEmpty() const { return m_pObj == NULL; }

void Set( T* pObj ) { m_pObj = pObj; }
void Empty() { m_pObj = NULL; }

T* GetPtr() const { return m_pObj; }

operator T*() { return m_pObj; }

T& operator*() { return *m_pObj; }
T* operator->() { return m_pObj; }

void operator=(const IGPtr<T> rhs) { m_pObj = rhs; }
void operator=(T* rhs) { m_pObj.Set( rhs ); }

protected:
T* m_pObj;
};


I want to implicitly upcast between IGPtr types, and I want the code to know that I want to do this, and do it automatically VIA the IGPtr class. I'm sure this is possible, but I get the following error:

1>d:\project\igsceneloader.cpp(286) : error C2664: 'IGScene::AddActor' : cannot convert parameter 1 from 'IGPtr<T>' to 'IGPtr<T>'
1> with
1> [
1> T=IGActor
1> ]
1> and
1> [
1> T=IGNode
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called


Any advice really appreciated! Thanks!!

Share this post


Link to post
Share on other sites
Advertisement
I have never heard of term "courier" applied to pointers. Nor has Google.

I want to implicitly upcast between IGPtr types, and I want the code to know that I want to do this, and do it automatically VIA the IGPtr class. I'm sure this is possible, but I get the following error:[/quote]

Compile-time vs. run-time polymorphism.

When you declare P<A> and P<B>, they end up as distinct types - they cannot be converted from one another, they are the same as A and B - no common ancestry.

There are two solutions:struct BasePtr;
template < typename T > struct IGPtr : BasePtr;

void foo(BasePtr p);
This would be run-time polymorphism with base class. Resolution here happens during run-time, it might be necessary to use dynamic_cast and type safety is limited to whatever BasePtr can provide, T cannot be explicitly checked during compile time.

Alternative is:
template < class T >
void foo(IGPtr<T> p);
Compile-time polymorphism, which will instantiate separate functions for each distinct T passed. This is how STL is designed.

Share this post


Link to post
Share on other sites
Alternately, add a template constructor to your smart pointer class. Such as

template <typename U> IGPtr( U * pObj) : m_pObj( pObj ) {}

That said, I don't see why you don't just use regular pointers here. Your smart pointer class doesn't seem to add anything.

Share this post


Link to post
Share on other sites
Sorry for the delay - was busy giving that a try!

That was fantastic - a clear, patient, perfect answer :D This is why I love gamedev.net :) thankyou for that!

I did wonder why Google didn't like 'pointer courier' - I thought that was the term for this concept? I guess not :P

I'm certain I want the former, run-time polymorphism, as I want the held pointer to be treated as the base class if that's what the function expects - I'm defnitely going to have to avoid dynamic_cast though, but this should be OK as all the functionality I'm after at this point should all be in the base class. The only wonder I have at this point is whether or not pure virtual functions would be called on these IGPtr objects - there's only one way to find out :P

Share this post


Link to post
Share on other sites
Hi SiCrane - it doesn't... yet - I'm building up a codebase to give my own ref counted pointer implementation a go - I've always been curious about ref counted pointers, and while there are a million perfectly decent implementations, I wanted to do it myself, to learn from the experience :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!