Re: Few design questions and auto_ptr
Soumen wrote:
For one of my project, I want to use auto_ptr. But I'm facing few
issues.
1. Say I've Controller class. I don't want to allow user to create
object of this class unless all arguments
are valid. So I've made the constructor private and given one
static method to create an object of this
class. Since one of senior members in the team doesn't like
throwing exception, I've made this static
method to return NULL pointer when arguments are not valid. But
that stops using auto_ptr since it
cannot accept NULL, I guess. Is there any better way to do this?
No, it accepts
as auto_ptr has a constructor
explicit auto_ptr(X* p =0) throw();
and you can call "get()" to test if the underlying pointer is null or not.
2. Now, I've a Cache class in which few members are auto_ptr. Those
basically some pointer to few
why dynamic allocating map/set here?
why not just use map/set as member?
I think you only need to use pointer as template argument for set/map.
STL type map or set etc. Now users of this Cache class needs read
access to these data. Is returning
a const reference a good idea? Returning the member itself will
cause transfer of ownership and I guess
you mean returning an auto_ptr<Type>?
yes, ownership is transferred.
returning a pointer is not a good idea since the user has control
of destroying the object pointed by it.
you can't avoid everyone to do a evil thing with a programming language.
One can also delete a pointer taken from the const reference as well.
if reference satisfies your need, mainly you never return a null pointer
to represent a failure, then returning a const reference of Type is fine.
But I guess a Cache can't guarantee that every query operation will success.
Let me know your views. This may be trivial design issues to most of
you. But I'm not an expert in
design issues. Thanks in advance to all your help.