Jump to content
  • Advertisement
Sign in to follow this  
Kurioes

smart pointer trouble

This topic is 4863 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

I made a smart pointer (that does reference counting), and I'd like to do the following; Suppose I have a class CFoo, and a class derived from CFoo named CBar. I have two smart pointers; SmartPtr<CFoo> foo and SmartPtr<CBar> bar. Suppose bar holds a valid pointer. My (templated) smart pointer can't do this; foo = bar Is there any way to solve this? If so, how? My smart pointer (cRefHandle, bottom of file) definition can be found at http://king0r.dyndns.org/svn/kbl/kbl/base/refcount.h Any help is welcome!

Share this post


Link to post
Share on other sites
Advertisement
A templated operator= will preserve type-safety and allow you to implicitly cast just like a regular pointer.

class Foo
{
};

class Bar : public Foo
{
};

class Blah
{
};

template<class Type>
class Ptr
{
public:

template<class NewType> Ptr & operator=(Ptr<NewType> & nt)
{
mPtr = nt.mPtr;
return *this;
}

Type * mPtr;
};

void main()
{
Ptr<Foo> foo;
Ptr<Bar> bar;
Ptr<Blah> blah;
foo = bar; //works fine
foo = blah; //throws compile error
system("pause");
}

Share this post


Link to post
Share on other sites
I tried it; the compiler complains that I cannot access nt's private member mPtr (which, in my case, indeed is private). Is there a workaround for this problem?

Nearly there though... Thanks!

Share this post


Link to post
Share on other sites
I haven't tried this, but perhaps you could do something like this?


template <class T>
class Temp
{
friend class Temp;

int m_value;

public:

Temp(int value) : m_value(value)
{

}

template <class U>
void Method(const Temp<U>& temp)
{
cout << temp.m_value << "\n";
}
};





Using the friend keyword, you can allow all instances of Temp access to each others private members.

EDIT: However to preserve type safety, you should try to check to ensure that the cast is valid before you accept the pointer.

Share this post


Link to post
Share on other sites
Alternatively, add a...
_t * const GetPtr() const { return privateptr; }

...to your smart pointer declaration. It's not pretty, but it keeps your code safe (const-correct) and relieves you of the responsibility of making endless friend declarations.

Share this post


Link to post
Share on other sites
I tried it; the compiler complains that there is no acceptable conversion and something about ambiguous constructors :(

I could easily hack my way out by defining the () operator ( operator T*() ) but hiding the pointer is the point (pun not intended) of a smart pointer.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tim Cowley
Alternatively, add a...
*** Source Snippet Removed ***
...to your smart pointer declaration. It's not pretty, but it keeps your code safe (const-correct) and relieves you of the responsibility of making endless friend declarations.


I can't do that; it would expose the pointer which, I want to avoid :(
EDIT: i.e. "delete smartptr.GetPtr();" would mess up my reference counting.

Share this post


Link to post
Share on other sites
This subject is giving me a headache...

I'm pretty sure this problem is solvable, but I decided reference counting is not the way to go for what I'm doing (a GUI... refcounting every control is kind of stupid and unnecesary). I really only need the smart pointers for recourse management - I don't even need typecasting.

Thanks for all your help! (rate++)

I'm still interested in the solution though, or some good articles about templates (I'm considering buying this book; http://www.informit.com/title/0201704315).

Share this post


Link to post
Share on other sites
Quote:
Original post by Kurioes
I tried it; the compiler complains that there is no acceptable conversion and something about ambiguous constructors :(

I could easily hack my way out by defining the () operator ( operator T*() ) but hiding the pointer is the point (pun not intended) of a smart pointer.

Hmmm, Wavarian's solution worked for me. This is what you tried?

template <class _t>
class cRefHandle
{
friend class cRefHandle;
...

Share this post


Link to post
Share on other sites
Yes I did. The errors were probably generated by something else (my smart pointer class contains sone other operators and member functions). You could take a look at it if you want; the URL is in my first post.

p.s. It'll probably take a while (> day) for me to reply (provided I don't forget completely).

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!