Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to return a class object in a member function?

I'm quite new to programming in C++ so sorry if this is stupid. I have been working through the c++ primer book, and there is something i just cant get my head around. Take this function for example:

Sales_data& Sales_data::combine(const Sales_data &rhs)
{
    units_sold += rhs.units_sold;
    revenue += rhs.revenue;
    return *this;
}

And we called the function using:

total.combine(trans);

I get that the units sold & revenue in the total object will be combined with that in the trans object, working much like the compound assignment operator (+=).

I get that this would return the total object, but I dont get what returning the total object means...

like image 804
Kyle Blue Avatar asked Nov 30 '16 15:11

Kyle Blue


People also ask

How do you return an object from a member function?

A function can also return objects either by value or by reference. When an object is returned by value from a function, a temporary object is created within the function, which holds the return value. This value is further assigned to another object in the calling function.

What does returning an object do?

When you return pointer to it you are pointing to a specific area of memory within the object. Another option is to return copy of the value. It would mean make a copy and return it. The difference is that if you return pointer to objects internal variable that object state could be modified from outside.

How do I return a class object?

If a method or function returns an object of a class for which there is no public copy constructor, such as ostream class, it must return a reference to an object. Some methods and functions, such as the overloaded assignment operator, can return either an object or a reference to an object.

What is member function in class and object?

Definition. A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. Member function in C++ operates on any object of the class of which it is a member and has access to all the members of a class for that object.


2 Answers

Returning by reference (to the object being called on) means you could chain the calls in one line code; all these calls would be bound to the same object:

total.combine(trans).combine(trans2).combine(trans3);

which is equivalent with:

total.combine(trans);
total.combine(trans2);
total.combine(trans3);

(Of course it doesn't mean you have to call the same method, you could mix with other methods with similar characteristic.)

This idiom is often used in the implementations of operators like operator=, operator<<, operator>> etc, which are also possible to be called with chaining:

a = b = c;
cout << a << b << c;
like image 50
songyuanyao Avatar answered Oct 06 '22 00:10

songyuanyao


It returns a reference to itself, this allows method chaining. For example, say you have 2 Sales_data objects and you want to combine both of them, you can then "chain" the calls:

Sales_data s1, s2, total;
//stuff..
total.combine(s1).combine(s2);

Because you return a reference this allows the total object to be modified in between calls, which is why it's called a "chain".

like image 23
Hatted Rooster Avatar answered Oct 06 '22 00:10

Hatted Rooster