Archived

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

dave

YAY i got some smart-pointery-ness working!

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
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
oh yeah Arild, i did attempt a copy constructor for it and the operator = but for some reason on some occasions it compile ok then others i got warnings and erros, linker ones.

regards,

ace

Share this post


Link to post
Share on other sites
oh momma, just grab "Modern C++ Design". there''s even an excerpt bout smart pointers floating over the web.

Share this post


Link to post
Share on other sites
This is my latest smart pointer class, getting there me feels!


#include <stdio.h>

template <class T>
class SmartPointer
{
private:
T* obj;
public:
SmartPointer(T* p = 0) : obj(p)
{

}

SmartPointer()
{
obj = 0;
}

~SmartPointer()
{
if (obj)
delete obj;
}

T& operator *()
{
if (obj)
return &obj;
}

T* operator ->()
{
if (obj)
return obj;
}

inline operator =(const SmartPointer<T> &p)
{
if(obj)obj->Release();
obj=p.obj;
if(obj)obj->AddRef();
}

};


regards,

Ace

/*
~ Programming is creating problems then solving them ~

Level :- Beginner Intermediate
Currently Doing :- Background work on smart pointers, will then update 3D MineSweeper.
*/

Share this post


Link to post
Share on other sites
What Arild meant is that, given that your smart pointer will blow up if you ever make a copy, you ought to declare but not implement private copy constructor & assignment operator.

That way, instead of getting incorrect run-time behaviour (double delete), you get a compile-time error. That is, you design your class so that it purposefully doesn''t compile when used incorrectly.

class NonCopyable
{
private:
NonCopyable(const NonCopyable&);
NonCopyable& operator=(const NonCopyable&);
};


As for your comment about not using standard library facilities you do not fully understand, while laudable in theory, such a decision would crippling in practice if you adhere too strictly to it. For example, you don''t need to understand all the optimizations contained in the standard sort routine. All you need to know is that it''s a sort routine, that it has such and such interface, and behaves in such and such manner. Not using a library unless you''re able to re-implement it kinda defeats the point of having a library in the first place. (Now, pushing that to the extreme, you shouldn''t use a C++ compiler unless you''re able to write one on your own - good luck.)


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
tis true but of course there would be no point in implementing my own sorts etc, but i mainly mean Linked Lists and Smart pointers

regards,

Ace

/*
~ Programming is creating problems then solving them ~

Level :- Beginner Intermediate
Currently Doing :- Background work on smart pointers, will then update 3D MineSweeper.
*/

Share this post


Link to post
Share on other sites
delete 0;

is not an error so you don''t need to check:
if(obj)
delete obj;

also the following

if(obj)
return &obj;

only has one return path. What if there is no obj? what do you return?

use boost.smart_ptr. look it up

Share this post


Link to post
Share on other sites
I think that the most basic data structures and algorithms should be completely understood. By knowing how these are implemented and the quarks encountered with them, the programmer can use the libraries, which are nearly bugless, in a more inteligent way. With things as simple as smart pointers, it is a good excersize to try programming them to make sure you have a sound understanding of them. The more complex libraries or a compiler can be applied better if the user has an understanding of what it is doing. It would be over kill to reprogram one before you use it though.

Sorry for the semi-of topic post

Dwiel

Go see some hot chicks and vote to see who is hottest: dwiel.no-ip.com

Share this post


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

Share this post


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

And these are called ''smart'' pointer in contrast with your ordinary ''dumb'' C pointer, because they provide some extra functionality (automatic destruction, reference counting, etc.)

Though I''ll grant you, ace_lovegrove''s current implementation is flawed, as a number of posters have pointed out already.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why don''t you go do some research ...
http://www.relisoft.com/resource/resmain.html

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites