I've been overloading some methods for a template class.
This means I can write my code in this way.

myClass var3=var2+4;

Is it possible to do it the otherway around like

myClass var3=4+var2;

I know that the end result will be the same,
I was just wondering.

thanks

Well ,

myClass var3=var2+4;

This will work fine only if you provide a constructor taking an int argument or if you write down the operator+ to take in an int.

myClass var3=4+var2;

This will work i guess only if you provide a constructor.

But if you are not providing the constructor it wont work .

As I wrote, I've already implemented the "myclass + primitiveVal" overload.
And I don't need to use a constructor for this.

the RHS in my code is

//overloads '+' with a single value
template<class T>
Array<T>  Array<T>::operator+( const float f ){
  Array<T> tmp(x_);
  for(uint i=0;i<x_;i++) 
    tmp.data_[i] = data_[i] +f;
  return tmp;

And the LHS is a simple assign,

but can you elaborate on the signature for the other one?

If you want to be able to write

something = 4 + var;

You have to overload once more operator+.

First, declare it as friend of class, and then write it like:

Array<T> operator+ (const float f, Array<T> myVal);

That's because if you define operator+ as a member of class it is interpretting:
val + 3;
as:
val.operator+(3);
So if you write:
3 + val;
it is interpretted as:
3.operator+ //it's clearly rubbish

Comments
Very helpfull

Well i dont think the left hand side will work as the compiler actually searches to find a suitable function adding type <t> to a int in the std's

If you had a constructor
then it would have been

myclass var1=myclass(3)+var2;

I would anyways suggest to wait for anyone else to get back to this topic , but this is from what i know until now .

However a friend would be apt.

Edit:
Well missed by time

To all appearances you are trying to reinvent std::valarray class ;).
Look at <valarray> header of your C++ installation. For example, in MS VC++ 9 you can see:

template<class _Ty>
class valarray
{ // store array with various indexing options
...
    valarray<_Ty> operator+() const
    {    // return +valarray
        _VALOP(_Ty, size(), +_Myptr[_Idx]);
    }
...
};
...
template<class _Ty> inline
valarray<_Ty> operator+(const valarray<_Ty>& _Left,
	const _Ty& _Right)
{   // return valarray + scalar
    _VALOP(_Ty, _Left.size(), _Left[_Idx] + _Right);
}

template<class _Ty> inline
valarray<_Ty> operator+(const _Ty& _Left,
	const valarray<_Ty>& _Right)
{   // return scalar + valarray
    _VALOP(_Ty, _Right.size(), _Left + _Right[_Idx]);
}
...

So Sci@phy got you a good advice.

Yet another remark: object+int or int+object do not bear a relation to associativity. Left/right associativity is:

x ? y ? z evaluated as ((x ? y) ? z) // Left associative op ?
x ? y ? z evaluated as (x ? (y ? z)) // Right associative op ?

See http://en.wikipedia.org/wiki/Associative

Thanks,
it's working now thanks to your help.
The syntax for operator overloading is quite strange.

I'm doing a template matrix class,
And I was looking into using valarrays as rows.
But In the end I decided to do everything by myself.

Now is a good as time ever to learn this template stuff.

But thanks.

This question has already been answered. Start a new discussion instead.