Thread: subscipt overload (returning reference to array?)

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    2

    subscipt overload (returning reference to array?)

    Hi all,

    I'm a bit stumped about the idea of returning a reference to an array inside a struct. For example,

    Code:
    struct MyStruct
    {
         float x, y;
    
         float& operator [](int i)
         {
              return ((&x)[i]);  // this confuses me
         }
    I can use the overload to access x and y by using their corresponding subscripts. But how does that work? I understand incrementing a pointer to x will give me y if I dereference it. The overall syntax of that return confuses me.

    Hope someone can help me

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This:
    Code:
    (&x)[i]
    is equivalent to:
    Code:
    *(&x + i)
    Suppose i == 0, then we end up with:
    Code:
    *(&x + 0)
    which is equivalent to:
    Code:
    *&x
    which is equivalent to:
    Code:
    x
    Great, except that suppose i == 1, then we end up with:
    Code:
    *(&x + 1)
    Essentially we're treating x as if it were an array of 1 float object. Hence, &x + 1 is a pointer one-past-the-end of this "array". This is okay, but then we try to dereference this pointer, resulting in undefined behaviour.

    So, what you have here is an operator[] overload that only ever works if the argument is 0.
    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. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    2
    Thank you for your response. Let's say though I initialize a MyStruct object in main(), assuming I have a constructor,

    Code:
    int main()
    {
         MyStruct ms(0.1, 0.2);
         ms.[0] // == 0.1
         ms.[1] // == 0.2
    Those are the values I get for each subscript operator overload argument. Is that purely because when I initialize the constructor, each value is adjacent in memory?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, and of the same type.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning a reference == int(int (*)()) ??
    By manasij7479 in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2012, 10:39 AM
  2. Returning by reference
    By cuo741 in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2010, 07:06 AM
  3. returning by reference / value.
    By Kitt3n in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2010, 12:02 PM
  4. Returning a reference? Why?
    By Mr_Miguel in forum C++ Programming
    Replies: 12
    Last Post: 09-13-2007, 11:43 AM

Tags for this Thread