473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Few design questions and auto_ptr

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?

2. Now, I've a Cache class in which few members are auto_ptr. Those
basically some pointer to few
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
returning a pointer is not a good idea since the user has control
of destroying the object pointed by it.

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.

Regards,
~ Soumen
Jan 4 '08 #1
5 1697
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.
Jan 4 '08 #2
In article <7b**********************************@s27g2000prg. googlegroups.com>,
Soumen <so******@gmail.comwrote:
>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?
Yes, obviously there is a better way: use exceptions. :-)
Do you think the "senior member" might need to update himself maybe? :-(
Does the "senior member" realises that if current C++ is used,
exceptions can be thrown by new or any C++ standard library utility so
regardless if you are actually explicitly throwing exception in your
code, you still need to program in an exception safe way and catch any
exceptions thrown by the standard library?
Does the "senior member" realises that since you have to handle
exceptions anyway, you may as well use them in your code too rather
than have a duplicate error mechanism of exceptions _and_ error return
code (even worse error magic return value) and as such have the worse
of both world.

Obviously, there are some particular situations where exceptions might
be undesirable. In that case, there should be no exceptions whatsoever
in the system and only a subset of C++ with no exceptions at all
should be used. But most of the time, peoples that "don't like
throwing exceptions" are in deep need of retraining and are bad
mentors for junior programmers.

>2. Now, I've a Cache class in which few members are auto_ptr. Those
basically some pointer to few
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
returning a pointer is not a good idea since the user has control
of destroying the object pointed by it.
Assuming:

Cache
{
private:
std::auto_ptr<std::map<key_t, value_t m_theMap;
}

This is quite possibly a Java-ism that could be better rewritten most
of the time as:

Cache
{
private:
std::map<key_t, value_tm_theMap;
}

Unless you can give a good justification for using the first form, use
the second form.

The second thing you mention is getting access to this data. It
sounds as if you are thinking of:

Cache
{
public:
std::map<key_t, value_tconst & getInternatMapAccess()
{ return m_theMap; };
private:
std::map<key_t, value_tm_theMap;
}

and expect the client to directly query the std::map

A much better encapsulation would be to provide a querying interface
to your cache e.g.:

Cache
{
public:
value_t query(key_t key); // use const & if copying is expensive
private:
std::map<key_t, value_tm_theMap;
}

This way, your client is isolated of the Cache implementation. You
could use a std::map, std::set or external SQL database for your data
and the client wouldn't care. It also eliminates your problem since
by not giving access to your internal to a client, the client can't
mess with your internals.

Yan
Jan 4 '08 #3
yt******@nyx.nyx.net (Yannick Tremblay) wrote in
news:11***************@irys.nyx.net:
In article
<7b**********************************@s27g2000prg. googlegroups.com>,
Soumen <so******@gmail.comwrote:
>>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?
Why can't auto_ptr accept NULL?

Jan 4 '08 #4
Thanks to all for the responses. Sorry, I did a mistake that it
doesn't accept
NULL. Since I cannot use exception, I need to use return NULL in case
of error
and get() method to check against NULL.
On Jan 4, 7:29 pm, ytrem...@nyx.nyx.net (Yannick Tremblay) wrote:
In article <7ba14b44-c36e-47a9-9d25-bd09ab1ac...@s27g2000prg.googlegroups.com>,

Soumen <soume...@gmail.comwrote:
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?

Yes, obviously there is a better way: use exceptions. :-)
Do you think the "senior member" might need to update himself maybe? :-(
Does the "senior member" realises that if current C++ is used,
exceptions can be thrown by new or any C++ standard library utility so
regardless if you are actually explicitly throwing exception in your
code, you still need to program in an exception safe way and catch any
exceptions thrown by the standard library?
Does the "senior member" realises that since you have to handle
exceptions anyway, you may as well use them in your code too rather
than have a duplicate error mechanism of exceptions _and_ error return
code (even worse error magic return value) and as such have the worse
of both world.

Obviously, there are some particular situations where exceptions might
be undesirable. In that case, there should be no exceptions whatsoever
in the system and only a subset of C++ with no exceptions at all
should be used. But most of the time, peoples that "don't like
throwing exceptions" are in deep need of retraining and are bad
mentors for junior programmers.
2. Now, I've a Cache class in which few members are auto_ptr. Those
basically some pointer to few
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
returning a pointer is not a good idea since the user has control
of destroying the object pointed by it.

Assuming:

Cache
{
private:
std::auto_ptr<std::map<key_t, value_t m_theMap;

}

This is quite possibly a Java-ism that could be better rewritten most
of the time as:

Cache
{
private:
std::map<key_t, value_tm_theMap;

}

Unless you can give a good justification for using the first form, use
the second form.

The second thing you mention is getting access to this data. It
sounds as if you are thinking of:

Cache
{
public:
std::map<key_t, value_tconst & getInternatMapAccess()
{ return m_theMap; };
private:
std::map<key_t, value_tm_theMap;

}

and expect the client to directly query the std::map
Actually, I've set and a map. User would iterate over the set
and do find on map (set will have more entries than the map).
I need to return const reference of set and have a member
method to find on map.
A much better encapsulation would be to provide a querying interface
to your cache e.g.:

Cache
{
public:
value_t query(key_t key); // use const & if copying is expensive
private:
std::map<key_t, value_tm_theMap;

}

This way, your client is isolated of the Cache implementation. You
could use a std::map, std::set or external SQL database for your data
and the client wouldn't care. It also eliminates your problem since
by not giving access to your internal to a client, the client can't
mess with your internals.

Yan
Jan 7 '08 #5
In article <f2**********************************@p69g2000hsa. googlegroups.com>,
Soumen <so******@gmail.comwrote:
>>
The second thing you mention is getting access to this data. It
sounds as if you are thinking of:

Cache
{
public:
std::map<key_t, value_tconst & getInternatMapAccess()
{ return m_theMap; };
private:
std::map<key_t, value_tm_theMap;

}

and expect the client to directly query the std::map

Actually, I've set and a map. User would iterate over the set
and do find on map (set will have more entries than the map).
I need to return const reference of set and have a member
method to find on map.
I am not sure about this design. I don't clearly understand what you
are storing in your cache and how it is to be used by the clients but
it seems to me that the fact that you happen to have a set and a map
is more of a Cache implementation issues than a thing that is relevant
to the interface.

Is this relevant to the client that it needs to iterate over the set
for the actual task the client wants to achieve or couldn't you
simplify it to "the client wants the next value" e.g.:

Interface:
value_t getNextValue();

Implementation:
Iterate over the set
get the value from the map

Yan

Jan 7 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Rajat Chadda | last post by:
Given a pointer within an object, C++ runtime could have been written to delete the pointed object automatically. Why is it that they require a destructor to be written instead?
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
6
by: ccs | last post by:
Why should I use auto_ptr if all the "new" and "delete" handles things properly What is it for auto_ptr to have release()? When should call it? Why some people suggest using std::vector to...
10
by: ma740988 | last post by:
I was relieved somewhat when I saw a post by Andrei Alexandrescu highlighting an similar issue I'm faced with: So here's what Andrei Alexandrescu wrote: I code a generic object factory....
5
by: ma740988 | last post by:
Select parameters in a vendors API file is as follows: #ifdef __cplusplus typedef void (*VOIDFUNCPTR)(...); /* ptr to function returning void */ #else typedef void (*VOIDFUNCPTR)(); ...
17
by: Divick | last post by:
Hi, I am designing an API and the problem that I have is more of a design issue. In my API say I have a class A and B, as shown below class A{ public: void doSomethingWithB( B * b) { //do...
19
by: Joe Van Dyk | last post by:
I'm certain that there's some type of a design pattern that neatly solves my problem, but I dunno what it is. I need to create Message objects based on on the contents of a string (that's...
39
by: Andre Siqueira | last post by:
Hello all, I have a member function like thist: Query(const std::string & id, std::auto_ptr<Modifiermodif = std::auto_ptr<Modifier>()) when a try to instantiate a Query like ...
7
by: tradevol | last post by:
as this example, question: If my purpose is initialize data from xml files and store them in the vector, so they can be used in class B by other member functions, do you think functionP is a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.