# Operator qustion

## Recommended Posts

Mathius20    122
Hey, I was wondering what the difference between inline operator T*() const { return Object; } and inline T& operator *() const { assert(Object!=0 && "Tried to * on a NULL smart pointer"); return *Object; } I don't 100% know how to read operators. Like inline operator =(T *obj) can you read that as "the stuff in () is what comes after the = sign and the thing that comes before it is the objext?" and also(this is from Enginiuity article II) SmartPointer(SmartPointer<T> &p) { Object=0; *this=p; } The this pointer in this case is pointing to what? The object that is initialized? And I thought you couldn't change the this pointer, but in this it seems that the this pointer is changed to point to the smart pointer it recieves. I feel like I have alot of holes in my understanding with this, so sorry if these are dumb questions. Thanks for any help.

##### Share on other sites
tychon    652
The first operator is a conversion operator. When the class is used at a point that requires a T-pointer it calls the operator, which has a return type of T*. The second operator is the unary * operator, like you see in *variable, and it returns a T-reference.

And the operator = example you gave has no return type, which it must.

I've never really tried changing the this pointer directly, someone else will have to detail that one.

##### Share on other sites
Mathius20    122
Thx for the info.
inline operator =(T *obj)
{
if(Object)
{
Object->Release();
}
Object=obj;
if(Object)
{
}

}
is the whole thing. Like (T *obj) means what? I mean im trying to understand how to read it. like after the =() does that mean what comes after the = is whats in the ()? Like obj1 = obj2. So in this case obj2 would be what comes after the equals. I really dont know how to read it:(

tychon    652

##### Share on other sites
Fruny    1658
Quote:
 Original post by tychonI've never really tried changing the this pointer directly, someone else will have to detail that one.

You can't change the pointer itself, but *this = p is equivalent to this->operator=(p).

##### Share on other sites
tychon    652
Quote:
 Original post by FrunyYou can't change the pointer itself, but *this = p is equivalent to this->operator=(p).

Ahhh, completely missed the *. Makes sense.

##### Share on other sites
Mathius20    122
So with the = overloaded operator, it assigns from right to left. so when you say obj1 = obj2, the = operator gets called for obj2?

##### Share on other sites
tychon    652
Quote:
 Original post by Mathius20So with the = overloaded operator, it assigns from right to left. so when you say obj1 = obj2, the = operator gets called for obj2?

No. Try experimenting with things and see what the results are.

Here's a very short example, which as good practice you can expand it so that it handles all the standard arithmatic operations. If you want to take it a step further, have it handle all number data types and able to hold a number of unlimited size.

class Number{	public:		Number( int value ) : mValue( value ) {}				Number& operator =( const Number& num )		{ 			mValue = num.mValue; 			return *this;		}				friend std::ostream& operator <<( std::ostream& out, const Number& num );			private:			int mValue;};std::ostream& operator <<( std::ostream& out, const Number& num ){	out << num.mValue;		return out;}

Oh, and the : after the constructor is an initializer list, just in case.