&rhs != this, compare a reference to a pointer? - c++

this is an assignment operator. &rhs != this is confusing. my questions: rhs is a reference of Message type. What does &rhs mean? what does & do (a memory address of a reference?)?
Another question is about return *this . since we want a reference to type Message, but *this is a Message typed object, right? How can we return an object to a reference?
Message& Message::operator=(const Message &rhs)
{
if (&rhs != this)
{
some functions;
}
return *this;
}

&rhs means address of the object which reference is referecing to.
Message a;
const Message &rhs = a;
if (&rhs == &a) std::cout << "true" << std::endl;
This is will print true.
A reference is not a different object; it is just a syntactic sugar of pointer, which points to the same object whose reference it is. So when you write return this, it returns a pointer to the object, but if you write return *this, it returns either a copy of the object, or reference to the object, depending on the return type. If the return type is Message &, then you tell the compiler that "don't make a copy and instead return the same object". Now the same object is nothing but a reference. A reference of an object can be made anytime. For example, see the declaration of rhs above; it is const Message & rhs = a, since the targer type is mentioned as reference type, you're making a reference rhs of the object a. It is that simple.

Besides Nawaz's great answer, I want to point out that you have to be careful about returning a reference to a local variable which will go out of scope after function return. So avoid returning a reference like this:
string& foo()
{
string result = "abc";
return result;
}
which causes the following compiler warning:
reference to local variable result returned

A reference is just an alias to an object. References are formed by request of the function that is called; they are not (necessarily) part of an object's type. This is probably already familiar to you, but consider this:
void f1(int a) { ++a; }
void f2(int & a { ++a; }
int main()
{
int x = 5;
f1(x);
f2(x);
}
Surely you know the difference between the two functions. But note that x is always just an object of type int. Whether it is passed by reference or by value is not a property of x, but rather of the function.
The same goes for return types:
int q;
int g1() { return q; }
int & g2() { return q; }
int main()
{
++g2();
++g1(); // error
}
Again, q is just an object. Whether return q; returns it by value or by reference is not a property of q, but of the function. g1 makes a copy of q, while g2 returns a reference to the actual q object (which we can increment). (The return value of g1 cannot be incremented, precisely because it doesn't have a permanent existence and this would be meaningless (technially, the expression is an rvalue).)
So in you example, return *this; returns a reference to the object itself. That has nothing to do with this, but it has everything to do with the fact that the return type of the function is Message&.

Related

Returning a reference to an implicitly constructed parameter vs an implicitly constructed internal object

C++11 question
Trying to understand an issue I came across in our code. Not looking for the "right" way to do this, just want to know how this is supposed to work so I can figure out how to fix things in the future
I think function f1() is fine returning a reference to the temp implicitly constructed on the same line as the p1
But what about p2? The temp is constructed implicitly in the body of f2() when calling f1(), but f2() is returning the reference that is being returned by f1(). I thought the lifetime of the temp is extended to match the lifetime of the reference
asking because in one of our compilers p2 is garbage on the next time, but on the others it is not
struct PP
{
int i;
PP(int i_) : i(i_) {}
};
const PP &f1(const PP &p)
{
return p;
}
const PP &f2(int i)
{
return f1(i);
} // does the temp live here after return?
int main()
{
const PP &p1 = f1(1);
const PP &p2 = f2(2); // is p2 valid on the NEXT line
return 0;
}
I think this is undefined behaviour, and the fact that different compilers have different outputs would suggest that.
In f2, you're returning a reference constructed by f1, which itself has a reference to the local object i. At the end of the scope, i dissapears and invalidates the reference.
In fact clang-tidy detects this godbolt example
Also in reference initialization of cppreference it states, as an exception to lifetime extension:
a temporary bound to a return value of a function in a return
statement is not extended: it is destroyed immediately at the end of
the return expression. Such return statement always returns a dangling
reference.

Copy reference operator returning a reference not an object using the keyword 'this'

I want to ask a question about copy reference operators.
I have the following class named Mystring and I have a copy reference operator for it, which works.
#ifndef _MY_STRING_
#define _MY_STRING_
class Mystring
{
private:
char *str; // pointer to a char[] that holds a c-style string
public:
Mystring(); // no args
Mystring(const char *s); // overloaded
Mystring(const Mystring &source); // copy
~Mystring(); // destructor
Mystring &operator=(const Mystring &rhs);// overloaded copy assignment operator
void display() const; // getters
int get_length() const;
const char *get_str() const; // pointer is returned
};
#endif // _MY_STRING_
and this is the copy reference operator function:
// Overloaded assignment copy operator
Mystring &Mystring::operator = (const Mystring &rhs) {
std::cout << "Copy assignment" << std::endl;
if (this == &rhs) // check the pointer is on the same address of rhs i.e. & = reference operator
return *this; // returns the reference
delete [] this->str;
str = new char [std::strlen(rhs.str) + 1];
std::strcpy(this->str, rhs.str);
return *this;
}
I'm a beginner in C++ so this is confusing but I'll try to describe what happens.
I'm aware that there is a this operator, which acts as a pointer. When dereferenced, it allows us to work with the object itself.
From the first line
Mystring &Mystring::operator = (const Mystring &rhs)
I can see that I will have an operator function that returns a reference, as the & operator exists in the declaration.
However, at the end of the if statement and the overall function, we state
return *this
although if we are dereferencing this, we're returning the object and not the reference as per my explanation above.
Microsoft c++ documentation also states
The expression *this is commonly used to return the current object from a member function:
To clarify, this is a pointer to a current object so *this and then returning it should mean that i'm returning the object, not another reference.
What mistake am I making in my understanding of this code?
In C++ there are expressions whose evaluation just determines the identity of an object. They are called glvalue expressions. This is the case of the expression *this. this hold a value that represents an address in memory and the expression *this evaluates to the identity of the object that is at that address. This may be strange to call this an evaluation, this is just a designation: *this designates the object that is at the address hold by this.
A function that returns a reference, when called result in a glvalue: it just designates an object. In the case of the operator = this object is the one designated by the expression in the return statement *this.
To clarify, this is a pointer to a current object so *this and then returning it should mean that i'm returning the object, not another reference.
You are correct, this is the pointer and *this is the object the pointer points to.
Take a look, however, on how you deal with references. For example consider passing a value to a function by reference. It is done in a following way:
void foo(char &param) {
// do something
}
int main(int argc, char **argv) {
int x = 5;
foo(x);
return 0;
}
Note that the variable x is passed by reference but there is no & or * needed to indicate that to the compiler. This is because the compiler can figure it out by itself. This is also why you return *this and it is understood as reference.

Why cannot assign value to non-const reference?

Why these definitions are all ok:
int func(int p=255) {
return p;
}
int func1(const int &p=255) {
return p;
}
but this definition:
int func2(int &p=255) {
return p;
}
leads to compile error ?
What is the logic behind it ?
Taking arguments by reference means, you dont work with your local copy of the variable, but with a variable already defined in the scope of the calling function.
While your first example makes sense (you have a local variable p that you can fill with a default value) the second example is a bit more tricky: Usually when using references you expect the variable to have an address, since you want to modify it. For const-refernces, the compiler will still allow you to pass a literal, even if something like "reference to a literal" makes no sense at all.
In the third case the compiler expects you to modify p. But what part of the memory should this modification affect? "255" has no address - therefore it cant be used as a reference.
If you want to have a more detailed explanation, you should probably look for keywords like "rvalue" and "lvalue".
The attempted function definition
auto func2( int& p = 255 )
-> int
{ return p; }
… fails because you can't bind an rvalue to a reference to non-const. Basically that rule is because a simple value like 255 isn't modifiable. While the reference can be used to modify.
One simple solution is to express the default as a separate overload:
auto func2( int& p )
-> int
{ return p; }
auto func2()
-> int
{
int scratchpad = 255;
return func2( scratchpad );
}
A non-const reference must be bound to lvalue (i.e. its address could be got). 255 (i.e. an int literal) is not a lvalue, so int &p=255 fails.
A const reference could be bound to rvalue, and for this case, a temporary int will be created and initialized from 255. The temporary int's lifetime will be the same as the const reference.
int func(int p=255) {
return p;
}
p here is copied by value, and it is defined to exist in the scope of func.
int func2(int &p) {
return p;
}
// e.g. use:
int value = 10;
func2(value); // func2 *could* modify value because it is passed by non-const reference
In this case the compiler here expects p to have a name somewhere in memory (i.e. lvalue), so it can possibly write to it within func2. Passing by non-const reference allows you to modify the variable used in the function call. Since p must belong to someone else somewhere since it can be modified, you can't assign a default value to it.
But what about the const-reference case? Here, the compiler is smart enough to know that p can never be written to since it is const, so it doesn't need to have a name in memory to write to. In cases of a literal being passed (e.g. 255), it (behind the scenes) essentially creates a temporary and passes that temporary variable to the function.
int func1(const int &p=255) {
return p;
}
func1(10);
// Behind the scenes, the compiler creates something along these lines
// since it can never be modified.
const int some_temporary = 10;
func1(some_temporary);

Invalid initialization of non-const reference from a rvalue

MyObject& MyObject::operator++(int)
{
MyObject e;
e.setVector(this->vector);
...
return &e;
}
invalid initialization of non-const reference of type 'MyObject&' from an rvalue of type 'MyObject*'
return &e;
^
I am not sure what it's saying. Is it saying that e is a pointer, because it's not a pointer. Also, if you'd make a pointer to the address of e, it would get wiped out at the end of the bracket and the pointer would be lost.
Your return type is MyObject&, a reference to a (non-temporary) MyObject object. However, your return expression has a type of MyObject*, because you are getting the address of e.
return &e;
^
Still, your operator++, which is a postfix increment operator due to the dummy int argument, is poorly defined. In accordance to https://stackoverflow.com/a/4421719/1619294, it should be defined more or less as
MyObject MyObject::operator++(int)
{
MyObject e;
e.setVector(this->vector);
...
return e;
}
without the reference in the return type.
You're correct that e is not a pointer, but &e very much is a pointer.
I'm reasonably certain that returning a reference to a stack variable that will be out of scope before you can use it is also not such a good idea.
The general way to implement postfix operator++ is to save the current value to return it, and modify *this with the prefix variant, such as:
Type& Type::operator++ () { // ++x
this->addOne(); // whatever you need to do to increment
return *this;
}
Type Type::operator++ (int) { // x++
Type retval (*this);
++(*this);
return retval;
}
Especially note the fact that the prefix variant returns a reference to the current object (after incrementing) while the postfix variant returns a non-reference copy of the original object (before incrementing).
That's covered in the C++ standard. In C++11 13.6 Built-in operators /3:
For every pair (T, VQ), where T is an arithmetic type, and VQ is either volatile or empty, there exist candidate operator functions of the form:
VQ T & operator++(VQ T &);
T operator++(VQ T &, int);
If, for some reason, you can't use the constructor to copy the object, you can still do it the way you have it (creating a local e and setting its vector) - you just have to ensure you return e (technically a copy of e) rather than &e.
Change return &e; to return e;. In the same way that a function like
void Func(int &a);
isn't called with Func(&some_int) you don't need the & in the return statement. &e takes the location of e and is of type MyObject*.
Also note, MyObject& is a reference to the object, not a copy. You are returning a reference to e, which will be destroyed when the function finishes and as such will be invalid when you next make use of it.

Why does "return *this" return a reference?

In my C++ class they were discussing how to create an assignment operator. At the end of the assignment was the line "return *this," which they said returned a reference to the object that "this" points to. Why does it return a reference? If "this" is being dereferenced, shouldn't it just return the object?
A function returns a reference if its declaration (i.e. its signature) tells so.
So (assuming a class Foo) if a function is declarated
Foo fun();
then it returns a value (with copying, etc..)
But if it is declared as
Foo& fun();
or as
const Foo& fun();
a reference is returned.
The statement return *this; don't define by itself if a reference or a value is returned.
It returns the current instance of the type MyClass you are in. It's returned as reference because the assignment operator was explicitly told to return a reference.
MyClass& operator = (MyClass& other) { return *this; }
Note the & after MyClass as the return value. A reference is returned.
Unless the & weren't there right before operator, the instance would be returned by value.
The expression *this
At the end of the assignment was the line "return *this," which they said returned a reference to the object that "this" points to
They were wrong.
Why does it return a reference?
It doesn't.
If "this" is being dereferenced, shouldn't it just return the object?
Yes!
Dereferencing a pointer yields an lvalue. That means that the result of *this is the actual object that this points to. It's not a reference, but it's not a copy either.
[C++11: 5.3.1/1]: The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is “pointer to T,” the type of the result is “T.” [..]
It can be hard to conceptualise, since you can never do this yourself in code; it's just a feature of the * operator for native pointers, and has been since C.
Returning from operator=
Because you can't do it yourself, conventionally you'd bind that lvalue expression to a reference in order to use it in different contexts. For example, the expression *this in return *this gets bound to the return type of the function that you're returning from; in this case, an assignment operator.
Now, we could have the assignment operator return by value in which case an object copy would be made from the lvalue that comes from *this; however, for an assignment operator we usually return by reference so that we avoid an almost-certainly needless copy, and can perform chaining:
Type a, b, c;
c = b = a;
It's a convention with benefits, and no downsides. Can you think of a situation when you'd want op= to return by value?
Every dereferenced pointer is a reference to its pointee, else you'd 'loose' the pointee you're pointing to.
Invoke method twice on the same object, using a pointer and a reference:
MyClass* objPtr = .... ;
objPtr->doSomething();
MyClass& objRef = *objPtr;
objRef.doSomething();
Invoke method on distinct objects; original and copy:
MyClass* objPtr = .... ;
objPtr->doSomething();
MyClass objCopy = *objPtr; //here, the reference is passed to the (implicit or implemented) copy constructor if possible, else a compile time error occurs.
objCopy.doSomething();
That means, if you return a reference from an operator method which has MyClass (rvalue) instead of MyClass& (lvalue) as return type, a copy of *this (MyClass&) is created by reference (leaving aside return value optimizations and rvalue references).
This is useful for non modifying const methods such as + and - which have a new value as result while leaving the object on which this method was invoked unmodified.
Operators like += and your assignment operator modify the object inplace by convention and should therefore return a reference to allow expressions like primitive types support it, since a temporary copy may vanish and cause unexpected results:
Consider this expression:
int i = 4;
int r = (i += 3) <<= 2;
The result r is 28 (added and shifted inplace).
What is the value of i? 28 too, what else.
But what if hypothetically int::operator+= would return a copy of itself instead of a reference to itself?
The result r would be 28 too.
But the value of i?
It would be 7, since the inplace left shift was applied to a temporary int returned from the addition which gets assigned to r after that.
Continuing the assumption, the error may have the same effect (except for the value in i) as this expression:
int r = (i + 3) <<= 2;
But luckily, the compiler will complain, that he doesn't have an lvalue reference from (i + 3) to do the shift/assignment operation.
But play with this:
class Int
{
private:
int val;
public:
Int(int val) :
val(val)
{
}
Int operator+(const Int& other)const
{
return val + other.val;
}
Int operator+(int prim)const
{
return val + prim;
}
Int& operator+=(const Int& other)
{
val += other.val;
return *this;
}
//Error here, Int& would be correct
Int operator+=(int prim)
{
val += prim;
return *this;
}
Int operator<<(const Int& other)const
{
return val << other.val;
}
Int operator<<(int prim)const
{
return val << prim;
}
Int& operator<<=(const Int& other)
{
val <<= other.val;
return *this;
}
Int& operator<<=(int prim)
{
val <<= prim;
return *this;
}
operator int()const{
return val;
}
};
int main()
{
Int i = 4;
Int r = (i += 3) <<= 2;
cout << i;
return 0;
}
In C++ the * always means a value, in fact you can look to en English interpretation for these operators as follows:
&: the address of
*: the value of
So when you say &x it means "the address of x" and when you say *x it means "the value that x points to". So *this will always return a value.
Just be sure that the function itself that the hosts the returning is not a reference function. Please remember that in C++ you can create functions with the & or the * operators as well.