1

I'm working with NS2 that is in C++ language. I see the following code that I cannot understand it !!!!

ch->size() += IP_HDR_LEN;

thanks for your help...

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
Masoud Nazari
  • 476
  • 2
  • 6
  • 17
  • The function call returns something that can be incremented with `+=`. It would be an lvalue reference to something. – juanchopanza May 29 '14 at 12:46
  • depends on what ch->size() returns, could be a pointer, lvalue, reference etc.. what don't you understand it clearly looks like it is adding the length of an IP header length – EdChum May 29 '14 at 12:46
  • Find the type of `ch`, and show the declaration of the .size() function. Probably it returns a reference to a member variable – nos May 29 '14 at 12:46
  • Usually this means the `size()` function returns a reference to an internal member of `ch` whatever type this should be. – πάντα ῥεῖ May 29 '14 at 12:47
  • size() function return the size of packets, I didn't know that it's possible to use functions as the left operand !!!! – Masoud Nazari May 29 '14 at 12:49
  • 1
    The code is *not* using a function as the left-hand operand; it's using the *return value* from the function. – dlf May 29 '14 at 13:03

1 Answers1

7

The method ch->size() returns reference (lvalue) to something which is used in an expression.

For example:

class A{
 int x;
 public:
  int& getX(){ return x;}
};

then it can be used as

A* a= new A;
a->getX() +=5; // which is equivalent to x+=5 or x=x+5, since getX returns reference to 'x', it can be used as LHS of an expression

So for your question:

what does it mean when a function is used as left operand in C++

Here the return value is used as left operand, not the function. The return type is a reference to something, which represents lvalue, and can be used as LHS.

Edit

As pointed out by @dlf, size() can return an object (by reference or by value) of any class which overloads operator +=. If returned by reference, then it is same as the above example ( just int x becomes MyClass x, int& getX() becomes MyClass& getX() ). If returned by value, although one can, but of no use or lead to bad design ( if MyClasss operator += changes some global state).

Rakib
  • 7,435
  • 7
  • 29
  • 45
  • 1
    You are almost certainly correct about `size()` returning an lvalue reference. But if you want to be really pedantic about it (and hey, who doesn't?), it could be returning a class type that overloads `operator +=` too. – dlf May 29 '14 at 13:14
  • @dlf, I guess the answer covers it. You can just replace `int x` to `MyClass x`. Although one can return by *value*, instead of *reference*, it is of no use ( but one can, +1 for this). – Rakib May 29 '14 at 13:20
  • Your answer's fine. Like I said, just being pedantic :). The only possible "use" I can imagine for having the class object returned by value is if the overloaded += changed global/static state. That would be a horrible design, but I've seen worse! – dlf May 29 '14 at 13:23