YAY i got some smart-pointery-ness working!

Started by
27 comments, last by dave 19 years, 11 months ago
awite guys no need to slag off the code, im still learning

It''ll be a sorted smart pointer in a few weeks

take acer all and thanx for the negative ness!



regards,

Ace

/*
~ Programming is creating problems then solving them ~

Level :- Beginner Intermediate
Currently Doing :- Background work on smart pointers, will then update 3D MineSweeper.
*/
Advertisement
Why don''t you go do some research ...
http://www.relisoft.com/resource/resmain.html
Oh, and after you''re done digesting the above, start using existing smart pointer implementations. www.boost.org has a few in their library, and there''s always std::auto_ptr. Loki has a few, too, but I have no experience with that library.
Flipcode had a smart pointer submitted as a COTD. He was an experienced coder. Have a look at comments on his code.

http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-SmartPointersGM&forum=cotd&id=-1
Chris Brodie
thanx y''all, there areso many good pages out there hidden from search engines
quote:Original post by ace_lovegrove
But does it have to be a big pointer class? In Enginuity it look much more complicated than mine, but doesn''t entirely explain why.

Here''s min:

#include <stdio.h>template <class T>class SmartPointer{private:	T* ptr;public:	explicit SmartPointer(T* p = 0) : ptr(p) 	{		}	~SmartPointer()         	{		delete ptr;	}	T& operator *()				{		return &ptr	}	T* operator ->()			{		return ptr;	}};


regards,

ace


Shouldn''t that be
	T& operator *()				{		return *ptr; //not &ptr	}


I personally recommend Loki''s SmartPtr implementation; policy-based design owns =)
"Is life so dear, or peace so sweet, as to be purchased at the price of chains and slavery?" - Patrick Henry
quote:Original post by Fruny
quote:Original post by sirSolarius
What the heck is so smart about these pointers? It just looks like you made a standard pointer that deletes from the heap unconditionally when destructed (very very bad).


If that's so bad, care to explain why there is a smart pointer included in the standard library (std::auto_ptr) that does precisely that?



SmartPointer<int> a=new int;*a=2;while(true){    SmartPointer<int> b=a;    break;}*a=5;   // ERROR  


[edited by - sirSolarius on May 21, 2004 8:56:19 PM]
quote:Original post by sirSolarius
quote:Original post by Fruny
quote:Original post by sirSolarius
What the heck is so smart about these pointers? It just looks like you made a standard pointer that deletes from the heap unconditionally when destructed (very very bad).


If that''s so bad, care to explain why there is a smart pointer included in the standard library (std::auto_ptr) that does precisely that?



SmartPointer<int> a=new int;*a=2;while(true){    SmartPointer<int> b=a;    break;}*a=5;   // ERROR  


[edited by - sirSolarius on May 21, 2004 8:56:19 PM]

That''s exactly why the copy constructor and assignment operator should be declared private, in a smart pointer like the one ace_lovergrove made. So that it''s impossible to make mistakes like that. Once that''s taken care of there''s nothing bad about it, except that it''s not very flexible.

This topic is closed to new replies.

Advertisement