nonmember function swap used in assignment

Discussion in 'C++' started by ma740988@gmail.com, Oct 2, 2005.

  1. Guest

    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 []
     
    , Oct 2, 2005
    #1
    1. Advertising

  2. Guest

    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 []
     
    , Oct 2, 2005
    #2
    1. Advertising

  3. Guest

    || 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, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Erik Haugen

    nonmember vs member?

    Erik Haugen, Oct 24, 2003, in forum: C++
    Replies:
    13
    Views:
    641
    jeffc
    Oct 24, 2003
  2. Replies:
    1
    Views:
    280
    Victor Bazarov
    Jul 29, 2005
  3. nonmember function

    , Aug 1, 2005, in forum: C++
    Replies:
    3
    Views:
    303
    msalters
    Aug 3, 2005
  4. Niels Dekker (no reply address)

    What swap is called when using std::swap?

    Niels Dekker (no reply address), Jul 19, 2006, in forum: C++
    Replies:
    4
    Views:
    1,007
    Niels Dekker (no reply address)
    Jul 20, 2006
  5. desktop

    nonmember functions?

    desktop, May 23, 2007, in forum: C++
    Replies:
    8
    Views:
    396
    Gennaro Prota
    May 25, 2007
Loading...

Share This Page