Thread: Overloading the [] operator

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    7

    Post Overloading the [] operator

    Hello all,

    Ok, I'm fairly good at C, (and was starting to think I getting decent at C++) but when it comes to overloading I seem to have missed the bus.

    I'm trying to overload the [] operator and every time I use this operator it kicks into the overloaded function. My question is this: Is there a way to specify when I want to enter into the overloaded function?

    As it stands right now the function I want to assign to the overload is that it will sort the array and according to supscript will kick out that element of the sorted array. Yet the way it is now even when I create the original array it goes in and tries to perform the overloaded function.

    Any help would be appreciated greatly,

    Strahan, BTW I have the general code down it's just help differentiating between fn calls I need help with.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >I'm trying to overload the [] operator and every time I use this operator it kicks into the overloaded function

    This is the point in overloading operators.

    >Is there a way to specify when I want to enter into the overloaded function?

    Use the operator on an object that overloads it.

    >Yet the way it is now even when I create the original array it goes in and tries to perform the overloaded function

    Whenever you use the subscript operators the overloaded function will be called.

    You're not being very clear. Perhaps a brief outline of your code would help.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    wild guess

    I am only taking a wild guess and am assuming that you may have this issue

    you have and Array class which overloads the [] operator

    Code:
    int CFakeArray::operator [](int iNdex)
    {
     
    	if( iNdex >= 0 && iNdex < m_iCount )
    		return m_iElements[ iNdex ];
    
    
    	TRACE("index out of bounds");
    
    	return -1;
    }
    However when you use this object you are doing this...


    CFakeArray array[100];

    and you should do this...

    CFakeArray array;
    zMan

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    7

    Post

    Hiya,
    Thanks for the prompt reply.

    The only hting I'm still lost on is that my program uses the [] operator to find the nth smallest element within the array, and therein lies my problem, when I create the array it continues to hit this overloaded function and I'm trying to figure out how to keep it from doing that until the array is full and then use the [] to call the function to return the n'th smallest element from the sorted array (which this function is supposed to do).

    i.e.

    // create the array

    for ( x = 0; x < SIZE; x++ )
    {
    array[x] = whatever;
    }

    my problem is that every time it iterates thru the for loop it also hits the overloaded function which when called is to sort the array and return whatever element is in that position after the sort.

    i.e. 5th_smallest = array[4];

    should go up to the overloaded function and return that element from the sorted array.

    I've got the overloaded function written (I think<G>) but my program malfunctions when the array is created since it's always going into the overloaded []. Is there a way to keep it from doing that? Or am I making the problem more difficult thant I should?


    Thanks much,

    Strahan, heh I'm having a hard enough time explaining the problem so it'll make sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Need help on understandind arrays
    By C++mastawannabe in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2007, 10:50 PM
  3. Overloading [] operator for a vector
    By nappaji in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2007, 10:44 AM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Overloading the [] operator trouble
    By rmullen3 in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 12:46 PM