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.
*/
YAY i got some smart-pointery-ness working!
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
http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-SmartPointersGM&forum=cotd&id=-1
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 }
quote:Original post by Frunyquote: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 sirSolariusquote:Original post by Frunyquote: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
Popular Topics
Advertisement