Overloading Operators

This is a discussion on Overloading Operators within the C++ Programming forums, part of the General Programming Boards category; Is it possible to overload the subscript operator? If so, could someone please provide an example of doing so. I ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    73

    Smile Overloading Operators

    Is it possible to overload the subscript operator? If so, could someone please provide an example of doing so. I know that this must sound rather odd, but I am curious. Thank you in advance for any information you may provide! ^_^

  2. #2
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    Yes, here's an example from one of my programs.
    Code:
    template <class T> T& linked_list<T>::operator[](int index)
    {
    	if (index < 0 || index >= list_size) throw out_of_bounds();
    	node<T> *temp;
    	temp = front;
    	for (int i = 0; i < index; ++i)
    	{
    		temp = temp->next;
    	}
    	return temp->value;
    }
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,648

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Only the following operators cannot be overloaded:
    .
    .*
    ::

    Additionally, ?: cannot be overloaded.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    73
    Thank you all for your help! You have completly answered my question!

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Zach L.
    Only the following operators cannot be overloaded:
    .
    .*
    ::

    Additionally, ?: cannot be overloaded.
    The sizeof operator cannot be overloaded either, just to make the list complete

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    For completeness, neither can typeid.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 10:27 AM
  2. Replies: 16
    Last Post: 10-27-2007, 12:42 PM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM
  5. Overloading operators...
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 07:24 PM

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