Thread: Operator overloading issue - C++

  1. #16
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @laserlight -
    I think I didn't get some important things you described:
    What do you mean by copy assignment?
    You have also said that even if the const version is called in this assignment:
    Code:
    m1(0, 0) = m2(0, 0);
    The return value of m2(0, 0) is actually a copy of this element in the matrix - so what's the meaning of returning a const reference here?


    As for your last answer - it's right, but here, in our particular example, we didn't do any assignments/comparisons between objects but between ints...

    Thank you!!

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,514
    Quote Originally Posted by HelpMeC
    What do you mean by copy assignment?
    Basically, if an object of some type already exists, and you're assigning to it another object of the same type such that it becomes a copy of that other object, then what you have is copy assignment. For int objects, this could look like:
    Code:
    int x = 100;
    int y = 123;
    x = y; // copy assignment
    Quote Originally Posted by HelpMeC
    The return value of m2(0, 0) is actually a copy of this element in the matrix
    No, I wrote that "the return value of m2(0, 0) will be copy assigned to the object referred to by the return value of m1(0, 0)".

    Look back at my example above. This:
    Code:
    m1(0, 0) = m2(0, 0);
    is equivalent to:
    Code:
    x = y;
    but only because m1(0, 0) returns a non-const reference so that the element at 0, 0 in the matrix can be copy assigned to, and m2(0, 0) returns either a copy, a non-const reference, or a const reference so that it can be copied from.

    (Well, if you return a copy, then what you'll actually have is move assignment after the copy construction rather than copy assignment, but for ints, move assignment is exactly the same thing as copy assignment.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    wow.. don't understand anything

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,514
    So, you don't understand what is copy assignment?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @laserlight -
    First, I don't know what move assignment is actually - we haven't reached this topic yet.

    What I understood from all this const reference assignment is:
    Even if the const version is called in that assignment - the compiler somehow "casts" this const reference to int - to the int value itself (let's call it a copy in practice).

    Of course, in our particular case we can even assign to m1 something because it's non-const object. If it were a const Matrix object - we can't even assign to it anything.

    That's in principle what I understood...
    Thank you!

  6. #21
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @laserlight -
    Actually, I'm attending now the C++ class, and the instructor says that we declare for example two versions of operator[] such that:
    reference operator[](size_type n)
    const_reference operator[](size_type n) const

    And he is saying the first one is for when we call the operator[] in case the object is in the left side, and the const version is for when we call this operator in case the object is in the right side.

    But you have said that as long as the objects are non-const - the operator which will be called is the non-const one, no matter in which side any one of them.

    What's going on here?...

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,514
    Quote Originally Posted by HelpMeC
    What I understood from all this const reference assignment is:
    Even if the const version is called in that assignment - the compiler somehow "casts" this const reference to int - to the int value itself (let's call it a copy in practice).
    I would discourage understanding it as a cast. Rather, remember that a reference is an alias, i.e., it is another name for what it refers to. Furthermore, if you were to implement a copy assignment operator for a class type, you will find that canonically the parameter is of const reference type.

    Quote Originally Posted by HelpMeC
    Of course, in our particular case we can even assign to m1 something because it's non-const object. If it were a const Matrix object - we can't even assign to it anything.
    Yes, but remember that that's because the const overload for operator() is designed such that such assignment to change the content of the matrix is impossible.

    Quote Originally Posted by HelpMeC
    And he is saying the first one is for when we call the operator[] in case the object is in the left side, and the const version is for when we call this operator in case the object is in the right side.

    But you have said that as long as the objects are non-const - the operator which will be called is the non-const one, no matter in which side any one of them.

    What's going on here?...
    Your instructor is mistaken. You can prove that by writing a program, or changing one of the programs already discussed here to show that for this expression:
    Code:
    m1(0, 0) = m2(0, 0);
    where both m1 and m2 are non-const, the non-const operator() is called twice.

    EDIT:
    For example, you could compile and run this program:
    Code:
    #include <iostream>
    #include <vector>
    
    class Vector
    {
    public:
        typedef int value_type;
        typedef std::size_t size_type;
        typedef value_type& reference;
        typedef const value_type& const_reference;
    
        explicit Vector(size_type size) : data(size) {}
    
        reference operator[](size_type i)
        {
            std::cout << "non-const" << std::endl;
            return data[i];
        }
    
        const_reference operator[](size_type i) const
        {
            std::cout << "const" << std::endl;
            return data[i];
        }
    private:
        std::vector<value_type> data;
    };
    
    int main()
    {
        Vector v1(1);
        Vector v2(1);
        v1[0] = v2[0];
    }
    Last edited by laserlight; 12-26-2019 at 06:53 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @laserlight -
    You are of course right... I have shown your comment to my instructor, what led him to mail all the students he had a mistake explaining it that way.

    I think I got the big pic, but it seems like I can ask forever questions about that, like, okay, for the const version of this operator we return const reference which is just aliasing - so we are returning actually the very value of the reference eventually. So it becomes like a copy of that int assigned to another integer.
    But, we are returning const reference - but the constness of this reference has no effect on this copying process to my understanding - as we're eventually assigning a copy of the value..

    Thank you so much, you are amazing!!!!!!!!!!!!!!
    Last edited by HelpMeC; 12-26-2019 at 09:55 AM.

  9. #24
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    Quote Originally Posted by HelpMeC View Post
    @laserlight -
    You are of course right... I have shown your comment to my instructor, what led him to mail all the students he had a mistake explaining it that way.

    I think I got the big pic, but it seems like I can ask forever questions about that, like, okay, for the const version of this operator we return const reference which is just aliasing - so we are returning actually the very value of the reference eventually. So it becomes like a copy of that int assigned to another integer.
    But, we are returning const reference - but the constness of this reference has no effect on this copying process to my understanding - as we're eventually assigning a copy of the value..

    Thank you so much, you are amazing!!!!!!!!!!!!!!
    Now I think I understand the necessity of the const reference I talked about in the above comment.
    If a const member will invoke this kind of operator, and we for example return some of its data by reference, we want its data to be constant, so that's the reason to const the reference!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue overloading stream Operator/Manipulator
    By Jaken Veina in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2011, 12:48 PM
  2. Operator overloading compilation issue in GCC
    By redneon in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2011, 07:53 PM
  3. Overloading Issue
    By CornedBee in forum C++ Programming
    Replies: 7
    Last Post: 09-23-2007, 06:03 AM
  4. Simple operator overloading issue
    By Desolation in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2007, 08:56 PM
  5. Overloading << Issue
    By dld333 in forum C++ Programming
    Replies: 1
    Last Post: 10-27-2005, 02:18 AM

Tags for this Thread