Thread: Operator for array assignment.

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    Operator for array assignment.

    I know how to overload the array operator to GET a value:

    a = myClass[1];

    But how do i would i set the value like this:

    myClass[1] = a;

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    How does it make a difference ? You just have to make sure that myClass:perator [] returns a reference (which it should anyway).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, there should be two versions of myClass::operator[]. One returns a reference, the other returns a const reference.
    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

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    True. For const-correctness.

    Edit: The one which returns a const reference should also be a const function.

    Code:
    T_& myClass::operator [] (unsigned int i) { return mystuff[i]; }
    const T_& myClass::operator [] (unsigned int i) const { return mystuff[i]; }

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Ok heres my new problem. I cant return a reference -_-.

    Heres the code:
    Code:
        T operator[](int pIndex)
        {
            if(pIndex >= 0 && pIndex < count)
                return items[pIndex];
            return T();
        }
    If i change it to:

    Code:
        T& operator[](int pIndex)
        {
            if(pIndex >= 0 && pIndex < count)
                return items[pIndex];
            return T();  //<<< this doesnt work 
        }
    error: invalid initialization of non-const reference of type 'int&' from a temporary of type 'int'

    Where T is int
    Last edited by 39ster; 01-06-2008 at 05:57 PM.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Explain "doesn't work". It's Chinese to me.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have a choice: you can simply return the index and make it the caller's responsibility to provide a valid index, or you can check and throw an exception (e.g., std::out_of_range) if the index is invalid. The former is what operator[] for the standard containers do, the latter is what the at() member function for the standard containers do.

    You certainly cannot return a reference to a local temporary.
    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. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by 39ster View Post
    Code:
        T& operator[](int pIndex)
        {
            if(pIndex >= 0 && pIndex < count)
                return items[pIndex];
            return T();  //<<< this doesnt work 
        }
    error: invalid initialization of non-const reference of type 'int&' from a temporary of type 'int'

    Where T is int
    Right, because that way the compiler prevents you from doing this:
    Code:
    Myclass[-1]=a;
    If you want to allow the above, then returning a temporary is not enough. You have to actually put the newly constructed object in your array.

    Or you can decide on some other behavior for an invalid index as suggested by above.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Thanks. Also i just figured out what putting const at the end of const-safe functions does.
    Code:
        const T& operator[](int pIndex) const
        {
            return items[pIndex];
        }
    
        T& operator[](int pIndex)
        {
            return items[pIndex];
        }

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by 39ster View Post
    Thanks. Also i just figured out what putting const at the end of const-safe functions does.
    Ah yes, that const applies to the implicit 'this' parameter to non-static class functions. That's simply how you make 'this' a pointer to const.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM