• Advertisement

Archived

This topic is now archived and is closed to further replies.

YAY i got some smart-pointery-ness working!

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

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

Share this post


Link to post
Share on other sites
Advertisement
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).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Your code also provides no way to release ownership, or assign new ownership.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement