473,327 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

nonmember function swap used in assignment


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

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
|| 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andrew Koenig | last post by:
I am looking for the most effective way to exchange v and v, and expected to find something like v.swap(i, j) but it doesn't seem to exist. Is there anything better than v, v = v, v ...
13
by: Erik Haugen | last post by:
From reading gotw#84 (http://www.gotw.ca/gotw/084.htm), I'm convinced that I should try to make functions nonfriend nonmembers when practical, but then I came across this: Bruce Eckel says about...
12
by: RA Scheltema | last post by:
Hi all, I have the following code: namespace A { inline void func(int) { ...; } inline void func(float) { ...; } inline void func(char) { ...; } }
12
by: ypjofficial | last post by:
Hello All, I need to return a vector from a function.The vector is of int and it can contain as many as 1000 elements.Earlier I was doing //function definition vector<intretIntVector() {...
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
12
by: raj s | last post by:
What is the advantage and disadvantage of using function declaration with exceptions specified. like foo()throws(char&)
21
by: raylopez99 | last post by:
In the otherwise excellent book C# 3.0 in a Nutshell by Albahari et al. (3rd edition) (highly recommended--it's packed with information, and is a desktop reference book) the following statement is...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
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...
1
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....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.