Operator qustion

Started by
6 comments, last by tychon 17 years, 11 months ago
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.
Advertisement
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.
Thx for the info.
inline operator =(T *obj)
{
if(Object)
{
Object->Release();
}
Object=obj;
if(Object)
{
Object->AddRef();
}

}
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:(
I'd really recommend reading up on operator overloading.
Quote:Original post by tychon
I'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).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
You can't change the pointer itself, but *this = p is equivalent to this->operator=(p).


Ahhh, completely missed the *. Makes sense.
So with the = overloaded operator, it assigns from right to left. so when you say obj1 = obj2, the = operator gets called for obj2?
Quote:Original post by Mathius20
So 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.

This topic is closed to new replies.

Advertisement