Thread: operator overloading problem~

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    operator overloading problem~

    hi all~

    i built a class which supports operator overloading, here is my code:
    Code:
    class Test
    {
    	public:
    		Test() { a=2; b=3; };
    		Test operator * (Test t);
    		Test operator [] (Test t);
    		void trace() { std::cout << a << "\t" << b << "\n"; };
    		int a;
    	private:
    		int b;
    };
    Test Test::operator * (const Test t)
    {
    	a = a * t.a;
    	b = b * t.b;
    	return *this;
    }
    Test Test::operator [] (const Test t)
    {
    	std::cout << t.a << "\t" << t.b;
    	return *this;
    }
    This works fine like this: testInstance1 = testInstance2 * testInstance3
    but, it didnt work under this occasion: testInstance1 = 5 * testInstance3
    i have no idea how to overcome this, any suggestions ?
    Never end on learning~

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    in testInstance2 * testInstance3, the compiler is treating the * operator as a member function testInstance2. In your second example, it would treat the operator as a member function of 5, but 5 is not an instance of that class, and thus has not overloaded the operator to handle testInstance 3 as a parameter to this function. You could overload your operator once more to be able to accept an integer as a parameter, and then have:

    Code:
    testInstance1 = testInstance3 * 5

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You need to define an operator that works with integer values. Since the integer in your example is on the left hand side (lhs) you should make a friend function so it will work in that ordering. Remember your methods of that class have the implied this pointer, so if you defined a method as

    Code:
    Test Test::operator *(int rhs)
    Then your example wouldn't work, but

    Code:
    testInstance1 = testInstance3 * 5;
    would work fine.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    no no no guys, i know how to make that, my question is how to calculate when that integer appear left side, like: 5 * testInstance1 and not testInstance1 * 5, how to inplements a method which cope with this situation ?
    Last edited by black; 07-19-2004 at 11:12 PM.
    Never end on learning~

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Why don't you re-read my reply, I clearly told you how if you were paying attention.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Quote Originally Posted by MrWizard
    You need to define an operator that works with integer values. Since the integer in your example is on the left hand side (lhs) you should make a friend function so it will work in that ordering. Remember your methods of that class have the implied this pointer, so if you defined a method as

    Code:
    Test Test::operator *(int rhs)
    Then your example wouldn't work, but

    Code:
    testInstance1 = testInstance3 * 5;
    would work fine.
    hey thanx man ! and why we should consult friend method here ? can any example be showed plz ?
    Last edited by black; 07-20-2004 at 07:10 AM.
    Never end on learning~

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    friend Test operator::*(int, Test);
    friend Test operator::*(float, Test);
    friend Test operator::*(double, Test);

    The compiler will evaluate the type of the lhs member. If that type has method available to deal with the rhs, then it will use it. If not, it will try to do a conversion of the rhs to an acceptable type. However, we can't overload members for primitive types. So, writing a method for int won't work. You can however, create a friend function for the type of rhs. Since rhs will be the class in question, it also allows the function to use private members of the class as if it were a method of the class.

Popular pages Recent additions subscribe to a feed