nonmember function swap used in assignment

M

ma740988

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::eek:perator=(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 [[email protected]]
 
I

int2str

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
 
M

ma740988

|| 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top