Subscript operator overloading woes (const related?)

This is a discussion on Subscript operator overloading woes (const related?) within the C++ Programming forums, part of the General Programming Boards category; I have the following code: Code: struct TwoInts { int val[2]; int &operator [] (unsigned int); TwoInts(int, int); }; int ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    125

    Subscript operator overloading woes (const related?)

    I have the following code:
    Code:
    struct TwoInts
    {
    	int val[2];
    	int &operator [] (unsigned int);
    	TwoInts(int, int);
    };
    
    int &TwoInts::operator [] (unsigned int i) { return val[i]; }
    
    TwoInts::TwoInts(int a, int b) { val[0] = a; val[1] = b; }
    
    TwoInts operator + (const TwoInts &a, const TwoInts &b)
    {
    	return TwoInts(a[0]+b[0], a[1]+b[1]);
    }
    
    int main(int argc, char *argv[]) { return 0; }
    When I compile it, the only line of the + operator gives the following error four times:
    14 C:\test\main.cpp passing `const TwoInts' as `this' argument of `int& TwoInts::operator[](unsigned int)' discards qualifiers
    This despite the fact that it seems to me I'm doing nothing wrong. Sure, a and b are const, but I'm not modifying them, nor am I treating the float returned by the [] operator as non const. Any way to fix this?
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You're trying to call a non const member function on a const reference.
    You could try this way
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct TwoInts
    {
    	int val[2];
    	
    	int operator [] (unsigned int) const;
    	int & operator [] (unsigned int);
    	
    	TwoInts(int, int);
    };
    
    int  TwoInts::operator [] (unsigned int i) const { return val[i]; }
    int & TwoInts::operator [] (unsigned int i) { return val[i]; }
    
    TwoInts::TwoInts(int a, int b) { val[0] = a; val[1] = b; }
    
    TwoInts operator + (const TwoInts &a, const TwoInts &b)
    {
    	return TwoInts(a[0]+b[0], a[1]+b[1]);
    }
    
    int main(int argc, char *argv[]) { 
        return 0; 
    }
    Kurt
    Last edited by ZuK; 05-05-2007 at 11:37 AM.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Ah, now it compiles without errors.
    I take it it automatically uses the const function whenever it's called on a constant instance, and always uses the non-const function otherwise?
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,326
    Yes. The rule of thumb is that subscript operators often come in pairs.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  3. Basic things I need to know
    By maxorator in forum C++ Programming
    Replies: 53
    Last Post: 10-15-2006, 04:39 PM
  4. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21