-2

I wonder, why don't we use only this without * in case of an overloaded assignment operator? If I use this only, it gives me a compiler error. Why we don't use * with arr[i], or any other variables, like return x in the 2nd and 3rd example?

Myclass &Myclass::operator=(const Myclass &rhs)
{
    if(this==&rhs)
        return *this;
} 

double& setValues( int i ) {
    return vals[i];   // return a reference to the ith element
}

int& setValues(int x) {
    return x;   // return address/reference  of x
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
logan_92
  • 117
  • 9
  • 7
    because `this` is a pointer to the current object, so if you want a reference to a object, you have to dereference it – Alberto Sinigaglia Jul 30 '20 at 20:57
  • 2
    `*x` means `x[0]` – M.M Jul 30 '20 at 20:57
  • 2
    @M.M While correct, I think that's only going to cause more confusion here – Mooing Duck Jul 30 '20 at 21:00
  • so in case if i want to return pointer (by address) , do i still need to use *this??? Myclass *Myclass::operator=(const Myclass &rhs) { if(this==&rhs) return *this; } – logan_92 Jul 30 '20 at 21:01
  • 3
    Note that example 3 looks like a bomb. It returns a reference to `x`, and `x` is a local variable. – user4581301 Jul 30 '20 at 21:01
  • 1
    @user4581301 you are correct , i should make x global before it destroyed by function termination – logan_92 Jul 30 '20 at 21:03
  • In the given example you do **not** want to return reference to a pointer to an instance. You want to return the instance **at** the pointer. – user4581301 Jul 30 '20 at 21:03
  • @MooingDuck if someone is confused by `*x` and `x[0]` then now would be a good time to try and gain understanding – M.M Jul 30 '20 at 21:04
  • @user4581301 so if i want to return pointer , i will just return this because it's already pointer and the function will return the pointer address of this (object) , so if i want to use it outside function i will need to *deference it – logan_92 Jul 30 '20 at 21:06
  • how is this an rvalue ??? this has an address in memory for the current object??? – logan_92 Jul 30 '20 at 21:08
  • 1
    Weird as it sounds, sometimes you do want to return a reference to a pointer just like sometimes you need to pass a reference to a pointer, but as MM pointed out above you can't return a reference to `this`. Wrong type of variable. – user4581301 Jul 30 '20 at 21:08
  • 2
    @logan_92 it's a strange thing, but `this` is just plain strange. If I remember correctly, the only reason `this` is a pointer is references didn't exist when `this` was added to the language. – user4581301 Jul 30 '20 at 21:10
  • @user4581301 thank you for clarification but C++ is so weird – logan_92 Jul 30 '20 at 21:12
  • @M.M thank you for the clarification – logan_92 Jul 30 '20 at 21:12
  • @M.M *x means x[0] , do you mean *x refer to firs element of the array because the first element of the array is const pointer ??? – logan_92 Jul 30 '20 at 21:16
  • 1
    `this` is a pointer to the instance. Think of the chaos you could cause if you returned a reference to `this` and then some er *changed where it pointed*. What would that even mean? The object you passed into the function is still where it was. Could you damage it's `this`? Probably a really good Q&A for this already on SO. I'll see if I can find it. – user4581301 Jul 30 '20 at 21:16
  • @user4581301 ok for 1st example in my code when i return *this , this means i return the object itself (address of object??) , but if i return this => it means i just return the address that exist in this pointer. is this correct???? – logan_92 Jul 30 '20 at 21:20
  • 1
    *i return the object itself* Yup. No address involved. A pointer is just another variable, but instead of holding a number or whatever, it holds the address of another object. If you return a pointer, you return a copy of the address. If you return a reference to a pointer, you return the pointer and the receiver can modify where it points. – user4581301 Jul 30 '20 at 21:26
  • 1
    Reading on that `this` is an rvalue stuff: https://stackoverflow.com/questions/7341607/so-what-is-the-type-of-this-why-is-this-not-a-lvalue , https://stackoverflow.com/questions/27095476/is-this-pointer-an-r-value, and https://stackoverflow.com/questions/6067244/type-of-this-pointer – user4581301 Jul 30 '20 at 21:28
  • 1
    @user4581301 so in case of *this => this will return a reference to this object in my stack. and reference is as an alias for this object. if i use this => it will just return an address of this pointer and this address points to the object , so if i want to access the object i will need to * pointer – logan_92 Jul 30 '20 at 21:31
  • thank you @user4581301 , i think i got it – logan_92 Jul 30 '20 at 21:31

1 Answers1

3

@logan_92: References, and pointers are a bit tricky: House& is a reference to a house somewhere ("That is Bob's house over there"). House* is a building address ("13931 Main Street"). Dereferencing a pointer is like driving to the building.

Likewise: this is a pointer. It is the address of a Myclass, it is not a Myclass itself. operator= returned a reference to a house, so you dereference it to go to the Myclass&, so that you can return the reference. It has nothing to do with being "inside the class" or "outside the class". The only difference is an address vs a reference.

One of the things you can do with pointers and addresses is get an offset. ("The house 3 down from 13931 Main Street"). The basic syntax is *(pointer+3). This turns out to be incredibly handy, so C made a special syntax for this: pointer[3]. It is a reference to the third object over from the address of pointer. As a result, this[0] is the same as *this. It's 0 objects over from the address.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • thank you for the answer . it cleared a lot of confusion, but i'm just new to programming so maybe i'm asking stupid questions , this[0] => it means the object itself ??? i wounder what will it return (i think it return the name of the object and because operator= return a reference to the object so it will return the address of this object)?? and also this syntax this[0] like you treat object as arrays??? so if i write this[1] what will it access??? – logan_92 Jul 30 '20 at 23:09
  • 1
    pointers and arrays [have an interesting relationship](https://stackoverflow.com/questions/26517164/why-am-i-being-told-that-an-array-is-a-pointer-what-is-the-relationship-between). For pointer `p`, `p[N]` is just a [pretty way](https://en.wikipedia.org/wiki/Syntactic_sugar) of writing `*(p+N)`. So if `N` is 0, `*(p+0)`-> `*(p)`-> `*p`. So `this[0]` will be `*this`. – user4581301 Jul 31 '20 at 04:38
  • 1
    `p[1]` is whatever's in storage after `*p`. If you have an array, `p[1]` makes sense. If not, you've indexed outside of [the object](https://en.cppreference.com/w/cpp/language/object) (Read the link because in C++ object doesn't mean what you probably think it does) and get to experience the joys of [Undefined Behaviour](https://en.cppreference.com/w/cpp/language/ub) first hand. This means that `this[1]` is the storage after `this` and frankly I've never delved deep enough into the corners of C++ to know if it's legal to do this even if `this` is part of an array. – user4581301 Jul 31 '20 at 04:43
  • [**class.this**](https://timsong-cpp.github.io/cppwp/class.this) and [**expr.prim.this**](https://timsong-cpp.github.io/cppwp/expr.prim.this) don't shed any light on that last bit. I'm going to leave this one for the language lawyers. – user4581301 Jul 31 '20 at 04:54
  • 1
    Interesting side note: Because `p[N]` is `*(p+N)`, you can perform some seriously deviant smurf like `N[p]` and get the same result. You'll probably never find a good reason to take advantage of this trick, and if you did it would probably fail code review. But it does show up in trick questions on exams and interviews. – user4581301 Jul 31 '20 at 04:57
  • 1
    `this` is just a pointer just like any other (mostly). So the C++ language doesn't know if it's a pointer at one element or a pointer at many elements, so it allows you to write `this[0]` just the same as `*this`. They're exactly the same, they're both a reference to the pointed-at object. A reference _isn't_ quite an address, it's merely a reference to an object somewhere. In many cases, the compiler just uses the object directly, wherever it is. – Mooing Duck Jul 31 '20 at 16:31