Hello all,
[All code in this post is here only for illustration and may not compile
as is]
I'm writing a class with value semantic, and I want its public interface
to provide the strong exception safety guaranty (if a public operation
fails for any reason, then an exception is thrown, and the object is
left in its previous state)
My class's data members are standard containers and built-in types. For
the question, let's assume
typedef std::vector< int vect;
class my_class
{
public:
my_class();
// ~my_class(); // Default implementation ok
// my_class( const my_class& ); // Default implementation ok
my_class& operator=( const my_class& );
public:
void modify();
private:
vect compute_modified_vect();
private:
bool my_bool;
vect my_vect;
};
vect my_class::compute_modified_vect()
{
vect new_vect;
// No special care about exception safety
// while modifying new_vect
return new_vect;
}
void my_class::modify()
{
compute_modified_vect().swap( my_vect );
m_bool = true;
}
my_class& my_class::operator=( const my_class& i )
{
vect new_vect( i.my_vect );
new_vect.swap( my_vect );
my_bool = i.my_bool;
return *this;
}
*The questions*
1) Do I really have to write operator= ? As far as I know, the default
copy assignment operator gives no guaranty about exception safety, but I
may have missed something.
2) Is my implementation of operator= correct ?
3) Since I thought very late in my conception that I may have to write
operator=, I wonder if there are other operations that I should write.
4) Is my implementation of my_class::modify correct ?
5) Any other suggestion/comment ?
Many thanks,
--
Vincent Jacques
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème"
Devise Shadock 1 1430
Alf P. Steinbach a écrit :
The above operator= should work nicely, but is too complicated for my
taste.
I'd just use the usual swap idiom,
Thanks for the suggestion. Providing a swap function is anyway something
that I would have had to do later.
Cheers,
--
Vincent Jacques
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème"
Devise Shadock This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gary.Hu |
last post by:
I was trying to catch the Arithmetic exception, unsuccessfully.
try{
int a = 0, b = 9;
b = b / a;
}catch(...){
cout << "arithmetic exception was catched!" << endl;
}
After ran the program,...
|
by: Scott Brady Drummonds |
last post by:
Hello, all,
My most recent assignment has me working on a medium- to large-sized
Windows-based C++ software project. My background is entirely on UNIX
systems, where it appears that most of my...
|
by: craig |
last post by:
I am wondering if there are some best practices for determining a strategy
for using try/catch blocks within an application.
My current thoughts are:
1. The code the initiates any high-level...
|
by: robinsand |
last post by:
Header File: car.h #if !defined CAR_H
#define CAR_H
enum TCarType { ctEconomy = 1, ctCompact, ctStandard, ctFullSize,
ctMiniVan, ctSUV };
class Car
{
public:
Car();
|
by: per9000 |
last post by:
Dear readers,
I have some problems with strong keys.
What I want to do is basically this.
A - create an application (f.x. strongHello.dll) with a strong key.
B - import the functions of this...
|
by: Sek |
last post by:
Hi Folks!
I was pondering over a code and noticed that exception handlers were
present in the private, protected as well as public methods. And,
ofcourse, public methods were calling priv/prot...
|
by: jehugaleahsa |
last post by:
Hello:
As an avid reader of C++ books, I know a lot of programmers out there
are confronted with the challenge of exception safety.
For those who don't know what it is, it is writing code in...
|
by: George2 |
last post by:
Hello everyone,
How do you understand the Bjarne's comments about exception
specification? Especially, "not required to be checked across
compilation-unit" and "violations will not be caught...
|
by: Ioannis Vranos |
last post by:
Perhaps a mechanism can be introduced in the C++0x/1x standard,
something simple like defining a function as:
void somefunc(void) throw()
{
// ...
}
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |