Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > nonmember function swap used in assignment

Reply
Thread Tools

nonmember function swap used in assignment

 
 
ma740988@gmail.com
Guest
Posts: n/a
 
      10-02-2005

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: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 [(E-Mail Removed)]

 
Reply With Quote
 
 
 
 
int2str@gmail.com
Guest
Posts: n/a
 
      10-02-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) 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 [(E-Mail Removed)]


 
Reply With Quote
 
 
 
 
ma740988@gmail.com
Guest
Posts: n/a
 
      10-02-2005
|| 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
nonmember functions? desktop C++ 8 05-25-2007 04:59 PM
What swap is called when using std::swap? Niels Dekker (no reply address) C++ 4 07-20-2006 08:44 PM
nonmember function bluekite2000@gmail.com C++ 3 08-03-2005 01:41 PM
Do nonmember nonfriend functions improve genericity? bluekite2000@gmail.com C++ 1 07-29-2005 05:00 PM
nonmember vs member? Erik Haugen C++ 13 10-24-2003 08:06 PM



Advertisments