the style of overloaded assignment operator
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: the style of overloaded assignment operator

Hybrid View

  1. #1
    Join Date
    Dec 2008
    Location
    Land Of Sand
    Posts
    7

    Thumbs up the style of overloaded assignment operator

    Hi

    I am thinking about the assignment operator...
    Basically if I want to overload it I code something like this :
    Code:
     
    class test{
    public:
    const test& operator=( const test& src ) { return *this; };
    };
    now the reason i return a const ref, is that i don't allow the user to do non-sence thing like the following :
    Code:
     
    test a, b, c;
    (a = b) = c;
    i just ommited the self assignment checking , cuz i wanna talk about the returned ref only in this thread....

    which style do you use ? this :
    Code:
    test& operator=( const test& src )
    or
    Code:
    const test& operator=( const test& src )

    I know that with non-const returned ref we could code somthing like this :
    Code:
     
    void func( test& obj ){};
    .
    .
    .
    func( a = b );
    is this a good style ?

    Khaled Alshaya, CS student at KFUPM ..... Just another programmer!

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: the style of overloaded assignment operator

    I've never really thought about it. Effective C++ is pretty much my programming bible and it doesn't give this idea a mention. You'll still get people doing assignment when they meant to do equality when you return a const& from operator=, so I guess I don't think it's worth it apart from if you are against chaining assignments.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Mar 2009
    Location
    Bulgaria
    Posts
    63

    Re: the style of overloaded assignment operator

    Quote Originally Posted by Mybowlcut View Post
    I've never really thought about it. Effective C++ is pretty much my programming bible and it doesn't give this idea a mention.
    As far as I remember it does. Or it was More Effective C++ but Scott Meyers is definitely the one who says "do like ints do".

    @Khaled.Alshaya, there are same famous C++ books like C++_How_to_Program_5_Ed that claim it should return a const reference, but I'd prefer to listen to Scott Meyers, who says that when we wonder about designing a type it's a nice idea to see how it is done in the standard library or in the built-in types. Well, it's legal to write "int i, j; (i = j) = 5;" so what your operator= should return is rather a non-const reference. Still you'll bump into people arguing about it, but as laserlight says "its disadvantage is mainly theoretical".

  4. #4
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: the style of overloaded assignment operator

    Quote Originally Posted by yzaykov View Post
    As far as I remember it does. Or it was More Effective C++ but Scott Meyers is definitely the one who says "do like ints do".
    In relation to the copy assignment operator he doesn't, but he does talk about what you mention in another item:
    Quote Originally Posted by Item 20: Prefer pass-by-reference-to-const to pass-by-value
    If you peek under the hood of a C++ compiler, you'll find that references are typically implemented as pointers, so passing something by reference usually means really passing a pointer. As a result, if you have an object of a built-in type (e.g., an int), it's often more efficient to pass it by value than by reference.
    I wonder just how small the overhead is though... it couldn't be much.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  5. #5
    Join Date
    Feb 2009
    Posts
    326

    Re: the style of overloaded assignment operator

    I think all of you have summed up pretty well.

    I have just pasted below my thoughts, correct me if I am wrong:

    1) returning by const reference is best if you intend not to change the value of the object returned.
    2) if you want to use the const reference returned object as an argument to func, it is possible (see below):


    Just define the function func to accept const reference as its argument:

    void func(const test& pTest);


    [code]
    #include <iostream>
    using namespace std;

    class test
    {
    public:
    const test& operator=( const test& src ) { return *this; };
    void func(const test& pTest);
    };


    int main()
    {
    test a, b, c;

    c.func(a=b);

    return(0);
    }

    void test :: func(const test& pTest)
    {}

    [\code]

    3) = operator is overloaded automatically for every class unless you want to perform something special other than just copying the values of the member variables.
    Last edited by Muthuveerappan; March 13th, 2009 at 03:29 AM. Reason: Incomplete text

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,266

    Re: the style of overloaded assignment operator

    Quote Originally Posted by Khaled.Alshaya
    which style do you use ?
    The former, since it is canonical while its disadvantage is mainly theoretical.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Mar 2009
    Location
    Bulgaria
    Posts
    63

    Re: the style of overloaded assignment operator

    You guys are getting on my nerves:
    Effective C++ 3rd edition
    Item 10: Have assignment operators return a reference to *this
    Read it an then talk about it.

    And something else... Mybowlcut, are you sure the compiler is always implementing references as pointers? I believe it's able not to allocate any memory and just add another name in the nametable to the same piece of memory when possible.

  8. #8
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: the style of overloaded assignment operator

    Quote Originally Posted by yzaykov View Post
    You guys are getting on my nerves:

    Read it an then talk about it.
    Whoa! Calm down there; no one is forcing you to read the thread. I misunderstood what item you were referring to at first, because you weren't clear..
    Quote Originally Posted by yzaykov View Post
    And something else... Mybowlcut, are you sure the compiler is always implementing references as pointers? I believe it's able not to allocate any memory and just add another name in the nametable to the same piece of memory when possible.
    Quote Originally Posted by Mybowlcut View Post
    Quote Originally Posted by Item 20: Prefer pass-by-reference-to-const to pass-by-value
    If you peek under the hood of a C++ compiler, you'll find that references are typically implemented as pointers, so passing something by reference usually means really passing a pointer. As a result, if you have an object of a built-in type (e.g., an int), it's often more efficient to pass it by value than by reference.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  9. #9
    Join Date
    Dec 2008
    Location
    Land Of Sand
    Posts
    7

    Smile Re: the style of overloaded assignment operator

    You see guys; Scott Meyers is saying do it as the built-in types did it. I don't understand why?!

    Cuz if I coded something like this:
    Code:
    test& operator=( const test& src )

    Then this statement is true!
    Code:
    (a = b) = c;

    after execution a == c only, which is not intended by a programmer who wants to do chaining... whereas if I return a const ref the compiler would issue an error saying that you can’t assign c to const a& ....

    I checked Absolute C++ and Effective C++, and they don't talk about the subject in detail,

    Maybe I am just dipping too much in a syntax difference which doesn't matter as laserlight said

    Thanks,
    Last edited by Khaled.Alshaya; March 13th, 2009 at 05:30 PM.
    Khaled Alshaya, CS student at KFUPM ..... Just another programmer!

  10. #10
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: the style of overloaded assignment operator

    Quote Originally Posted by Khaled.Alshaya View Post
    [COLOR=black][FONT=Verdana]Maybe I am just dipping too much in a syntax difference which doesn't matter as laserlight said
    Exactly. Besides, any programmer who writes a statement like that deserves a bit of a surprise.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  11. #11
    Join Date
    Mar 2009
    Location
    Bulgaria
    Posts
    63

    Re: the style of overloaded assignment operator

    You see, it's very unlikely to put braces in "(a = b) = c;" and not want this effect. C++ is a language where many things are allowed and if the programmer wants to do it this way, why put a ban on that? These braces mean that he wants it! Putting the const only limits the developers (in this very case!).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center