473,569 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1711
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************ *************** *******@s27g200 0prg.googlegrou ps.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<s td::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 & getInternatMapA ccess()
{ 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.ny x.net (Yannick Tremblay) wrote in
news:11******** *******@irys.ny x.net:
In article
<7b************ *************** *******@s27g200 0prg.googlegrou ps.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.ny x.net (Yannick Tremblay) wrote:
In article <7ba14b44-c36e-47a9-9d25-bd09ab1ac...@s2 7g2000prg.googl egroups.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<s td::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 & getInternatMapA ccess()
{ 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************ *************** *******@p69g200 0hsa.googlegrou ps.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 & getInternatMapA ccess()
{ 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
1199
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
2356
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. Two sub-classes have the relationship to communicate back and forth through this pointer. The pointer is responsible inside Top class for allocating...
6
1741
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 handle 2-dimension arrays? Does it have too much overhead compared to using "new" and "delete" directly? Thanks in advance!
10
1367
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. Obviously, the factory has a member function like CreateObject that returns a newly allocated object. The problem is, I break an important coding...
5
1603
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)(); /* ptr to function returning void */ #endif /* __cplusplus */
17
2580
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 something with b //possibly store in a list
19
470
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 actually in xml format, but it doesn't really matter). So, here's an example string (totally unrelated to the actual problem domain): Message Type:...
39
864
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 Query("123");
7
1417
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 viable function(will a could go away after out of the function)? If not, is there a better solution than using functionPt?
0
7698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.