Sign in to follow this  
Mathius20

Operator qustion

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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:(

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this