By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,948 Members | 1,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,948 IT Pros & Developers. It's quick & easy.

nonmember function swap used in assignment

P: n/a

So I'm reading the C++ coding standards(Shutter & Andrei), more
specifically item 56. There's a statement:
"Prefer to provide a nonmember swap function in the same namespace as
your type when objects of your type have a way to exchange their values
more efficiently than via brute-force assignment, such as if they have
their own swap or equivalent function. Additionally, consider
specializing std::swap for your own template types:"

namespace std {
template <> void swap(MyType& lhs, MyType& rhs) {
lhs.swap(rhs);
}
}
-------- end shutter/andrei

So I'm trying to generate a case where to just that. Provide my own
'non member swap function'. So consider:

// test case
# include <iostream>
# include <string>
# include <algorith>

using namespace std;

class msg {
std::string msg_id;
size_t val;
public:
explicit msg(
std::string const& msg_id_,
int val_
)
: msg_id(msg_id_)
, val(val_) {}

msg& msg::operator=(const msg& m)
{
msg temp(m);
swap(temp);
return *this;
}

void swap(msg &rhs) {
msg_id.swap(rhs.msg_id);
std::swap(val, rhs.val);
}

};

namespace std {
template <> void swap(msg& lhs, msg& rhs) {
lhs.swap(rhs);
}
}

class my_class {
msg *my_msg;
public:
explicit my_class() {}
void do_a_special_thing(msg& msg_)
{
//my_msg = msg_;
}
};

int main()
{
my_class m;
m.do_a_special_thing(msg("hi there" , 5));
}
the function do_a_special_thing should assing msg_ to my_msg utilizing
my 'non member swap function', but there's something amiss about my
approach here so always appreaciate the help.

// formerly [ma******@pegasus.cc.ucf.edu]

Oct 2 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

ma******@gmail.com wrote:
So I'm reading the C++ coding standards(Shutter & Andrei)
Great book, just finished it.
# include <algorith>
Missing 'm' in 'algorithm'
class msg {
Syle note:
Consider writing this out ('message') or otherwise marking this as a
class type (capital first letter maybe?). Otherwise variable namimg is
going to be difficult/confusing.
std::string msg_id;
Style note 2:
You're using a trailing _ for function arguments (ex. val_). While
there's certainly no "right" way of variable namimg, the very book you
were reading suggests the trailing _ for private member variables (like
this msg_id) [Coding Standards, Item 0].
msg *my_msg;
This should read 'msg my_msg' (no pointer). Otherwise the assignment
below won't work.
public:
explicit my_class() {}
You made the my_msg constructor that takes two parameters explicit. So
in my_class's constructor, you'll have to initialize my_msg with two
arguments somehow.

If you change the my_msg to be a msg (instead of a msg*), this would
work:

explicit my_class() : my_msg( "undefined", 0 ){}

You get the picture...
void do_a_special_thing(msg& msg_)
{
//my_msg = msg_;
}
If my_msg is not a pointer, this will work.

You could also use the srd::swap directly here:

std::swap( my_msg, msg_ );
m.do_a_special_thing(msg("hi there" , 5));
This doesn't work. Instead create the msg first, then call the func.:

msg hi( "hi there", 5 );
m.do_a_special_thing( hi );
the function do_a_special_thing should assing msg_ to my_msg utilizing
my 'non member swap function', but there's something amiss about my
approach here so always appreaciate the help.
It would help next time if you tried to compile your own code and refer
to the part of the code that you have problems with.

Hope this helps...

Cheers,
Andre
// formerly [ma******@pegasus.cc.ucf.edu]


Oct 2 '05 #2

P: n/a
|| the very book you were reading suggests the
|| trailing _ for private member variables (like
|| this msg_id)

:)

Ok I'll try your suggestions... For test purposes I generally overlook
all sorts of stuff. In any event, thanks

Oct 2 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.