smart pointer trouble

Started by
8 comments, last by Kurioes 18 years, 9 months ago
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!
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");}
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!
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.
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.
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.
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.
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).
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;...
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).

This topic is closed to new replies.

Advertisement