Thread: operators... which gets run first?

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    73

    operators... which gets run first?

    Code:
    template< class Type, UInt32 Elements >
    class MTrArrayType
    {
    private:
    	Type Array[Elements];
    
    public:
    	Type& operator [] ( UInt32 Index )
    	{
    		if( Index >= Elements )
    		{
    			cout << "Index out of range: " << Index << "/" << Elements << endl;
    			return Array[0];
    		}
    
    		return Array[Index];
    	}
    
    	operator Type*()
    	{
    		return Array;
    	}
    };
    This is my class. It's an ordinary array class, all it does is check if the index is out of bounds.

    When I put in...

    Code:
    MTrArrayType< int, 3 > MyArray;
    MyArray[1] = 6;
    ..., how do I know which operator function it calls? The one to change it to a Type*, or the one to do the index thing? If it's the one i don't want, how do I change it?

    Thanks!

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    73
    Hm. So it was that simple...

    I guess it makes sense.

    Alright, thanks for the help!

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    73
    Hey again. I just compiled that piece of code, and when I used it, it complained that 2 operators have similar overloads, or something like that. I know that theyre similar, but since one has higher precedence, shouldn't it know which one to use?

    Thanks!

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    overloading has nothing to do with operator precedence. Can you show the actual code because there is no overloading in what you showed.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    73
    My code up there has overloading in it. I overloaded the subscript and typecast operators. The compiler is compaining though...

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    *sigh* You don't have a complete program, where is the main? Also its not overloading in that case. Overloading is when you have multiple functions with the same name but different parameters.

    Without the operator[] or operator Type* functions you wouldn't be able to use those two at all. So post the actual code you are trying to compile.

    Edit: And include the exact error message.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Array syntax- array[i], is equivalent to *(array + i). It appears the compiler cannot figure out which of those versions to use.

    Did you mean to use operator *() instead of operator Type*()?

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You can ensure that operator [] gets called by doing this...

    MyArray.operator [](1) = 6;

    but it would be a better idea to do away with the conversion operator altogether. if you must then make an explicit function called to_array() or similar then where you would normally depend upon an implicit conversion you instead must state explicitly by calling to_array(). This has the effect of making your code safer and more readable. a win-win situation.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to run an exe command in c++ and get back the results?
    By mitilkhatoon in forum C++ Programming
    Replies: 5
    Last Post: 09-21-2006, 06:00 PM
  2. calculating the mode
    By bigggame in forum C Programming
    Replies: 10
    Last Post: 06-13-2006, 03:04 AM
  3. How I can Run exe file in C++
    By palang in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2006, 11:55 AM
  4. MSVC: run w/o debugger vs run w/ debuger
    By skorman00 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2006, 09:49 PM
  5. Replies: 2
    Last Post: 10-29-2002, 04:56 PM