What Does 'Return *This' Mean in C++

What does 'return *this' mean in C++?

this means pointer to the object, so *this is an object. So you are returning an object ie, *this returns a reference to the object.

What does 'return *this' actually do?

The return *this returns the current object (As a reference), which means you can now chain function calls on Sales_data if you called combine on a previous call. This means that something like this would work:

Sales_data t;
t.combine(a).combine(b).combine(c); // Assuming a, b, and c are other Sales_data's

This is a very useful technique which allows you to create a fluent interface which further allows for you to use things such as the named parameter idiom and is present in a variety of programming languages.

what does return; mean?

It's not going to return anything, you might have return statements in a void function to kind of alter the flow and exit from the function. ie rather than:

void do_something(int i)
{
if (i > 1) {
/* do something */
}
/* otherwise do nothing */
}

you might have:

void do_something(int i)
{
if (i <= 1)
return;

/* if haven't returned, do something */
}

How does the return statement work in C?

Your code is a little too minimal for comfort. I'd feel happier with:

int base(int a)
{
if (a == 1)
return 0;
return 37;
}

int inherit(void)
{
int n = base(1);
printf("Base(1) is %d\n", n);
return n + 3;
}

When inherit() calls base(), its current state is stored, the function base() is run, and it returns a value. In this code, the return value is captured in a variable, n and that is then used in the printf() call and the return within inherit(). That's how return works: it unilaterally stops the execution of the current function and continues in the calling function.

Even in main(), a return terminates the current function and returns a value to the calling function, which is the C runtime — and the C runtime ensures that the process exits, usually relaying the returned value to the 'environment' (a shell program on Unix, for example).

Note that the revised code ensures that base() always returns a value. Not doing so would lead to undefined behaviour in general. If the function is only ever called with the value 1 as an argument, it would be 'OK', but then why are you calling the function in the first place. So, always ensure that if a function is supposed to return a value, all returns from the function (including, in particular, the one at the end of the function) returns a value. In the original code, there is no return at the end — that's bad!

What does return {} statement mean in C++11?

return {}; indicates "return an object of the function's return type initialized with an empty list-initializer". The exact behaviour depends on the returned object's type.

From cppreference.com (because the OP is tagged C++11, I excluded the rules in C++14 and C++17; refer to the link for further details):

  • If the braced-init-list is empty and T is a class type with a default constructor, value-initialization is performed.
  • Otherwise, if T is an aggregate type, aggregate initialization is performed.
  • Otherwise, if T is a specialization of std::initializer_list, the T object is direct-initialized or copy-initialized, depending on context, from the braced-init-list.
  • Otherwise, the constructors of T are considered, in two phases:

    • All constructors that take std::initializer_list as the only argument, or as the first argument if the remaining arguments have default values, are examined, and matched by overload resolution against a single argument of type std::initializer_list
    • If the previous stage does not produce a match, all constructors of T participate in overload resolution against the set of arguments that consists of the elements of the braced-init-list, with the restriction that only non-narrowing conversions are allowed. If this stage produces an explicit constructor as the best match for a copy-list-initialization, compilation fails (note, in simple copy-initialization, explicit constructors are not considered at all).
  • Otherwise (if T is not a class type), if the braced-init-list has only one element and either T isn't a reference type or is a reference type that is compatible with the type of the element, T is direct-initialized (in direct-list-initialization) or copy-initialized (in copy-list-initialization), except that narrowing conversions are not allowed.

  • Otherwise, if T is a reference type that isn't compatible with the type of the element. (this fails if the reference is a non-const lvalue reference)
  • Otherwise, if the braced-init-list has no elements, T is value-initialized.

Before C++11, for a function returning a std::string, you would have written:

std::string get_string() {
return std::string();
}

Using the brace syntax in C++11, you don't need to repeat the type:

std::string get_string() {
return {}; // an empty string is returned
}

return NULL and return nullptr should be used when the function returns a pointer type:

any_type* get_pointer() {
return nullptr;
}

However, NULL is deprecated since C++11 because it is just an alias to an integer value (0), while nullptr is a real pointer type:

int get_int() {
return NULL; // will compile, NULL is an integer
}

int get_int() {
return nullptr; // error: nullptr is not an integer
}

In C++, what does & mean after a function's return type?

Yes, the int& version returns a reference to an int. The int version returns an int by value.

See the section on references in the C++ FAQ



Related Topics



Leave a reply



Submit