#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
YAY i got some smart-pointery-ness working!
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:
What''s it for?
(I''m not asking what smart pointers are for, I''m asking you what yours is for. That is, how you intend to use it, not why you made it in the first place).
(I''m not asking what smart pointers are for, I''m asking you what yours is for. That is, how you intend to use it, not why you made it in the first place).
What happens when you assign one pointer to another?
They will both have a member variable, ptr, which points to the same thing. When one of them is destroyed it will delete the thing pointed to. When the other one is destroyed it will delete it again... not what you want.
They will both have a member variable, ptr, which points to the same thing. When one of them is destroyed it will delete the thing pointed to. When the other one is destroyed it will delete it again... not what you want.
To AP & quorn : you''re assuming that he intends to use his smart pointer class to change ownership, or to have multiple smart pointers for a single object.
Maybe he''s just not needing these features, and decided not to support them. Hence my question about how he intends to use it
Maybe he''s just not needing these features, and decided not to support them. Hence my question about how he intends to use it
Doesn''t look that smart to me, as it calls delete in its destructor unconditionally - therefore if you were to copy the SmartPointer object explicitly or implicitly, the object would be freed twice, resulting in a crash.
This is what smart pointers are supposed to prevent, therefore I conclude that yours is not smart enough
Mark
This is what smart pointers are supposed to prevent, therefore I conclude that yours is not smart enough
Mark
quote:Original post by ToohrVyk
Maybe he''s just not needing these features, and decided not to support them.
Then he should at least have made a private operator= and copy constructor.
hey peeps im completely aware that this is no professional smart pointer, but the enginuity article goes through it so quickly without really explaining it. i know my smart pointer works when simply creating a pointer and using the pointee''s attributes, functions. I really do want to understand this smart pointer theory completely, as i think it is a huge step towards a more professional program/engine.
Im after somewhere that says why it works not "this is what u do".
Im NEVER planning to use the standard library stuff until i FULLY understand every aspect of it. I hope u can understand this.
Any sensible suggestions to anywhere than can help me with this.
Thanks all for replying,
regards,
ace
Im after somewhere that says why it works not "this is what u do".
Im NEVER planning to use the standard library stuff until i FULLY understand every aspect of it. I hope u can understand this.
Any sensible suggestions to anywhere than can help me with this.
Thanks all for replying,
regards,
ace
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement