When the expression

  In some cases, we need a variable only temporarily. For example, consider the following situation:

 

  12

  13int Add(int nX, int nY)

  {

  int nSum = nX + nY;

  return nSum;

  }

  int main()

  {

  using namespace std;

  cout << Add(5, 3);

  return 0;

  }

  In the Add() function, note that the nSum variable is really only used as a temporary placeholder variable. It doesn’t contribute much — rather, it’s only function is to transfer the result of the expression to the return value.

  There is actually an easier way to write the Add() function using an anonymous variable. Ananonymous variable is a variable that is given no name. Anonymous variables in C++ have “expression scope”, meaning they are destroyed at the end of the expression in which they are created. Consequently, they must be used immediately!

  Here is the Add() function rewritten using an anonymous variable:

  1

  2

  3

  4int Add(int nX, int nY)

  {

  return nX + nY;

  }

  When the expression nX + nY is evaluated, the result is placed in an anonymous, unnamed variable. A copy of the anonymous variable is returned to the caller by value.

  This not only works with return values, but also with function parameters. For example, instead of this:

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12void PrintValue(int nValue)

  {

  using namespace std;

  cout << nValue;

  }

  int main()

  {

  int nSum = 5 + 3;

  PrintValue(nSum);

  return 0;

  }

  We can write this:

  1

  2

  3

  4

  5int main()

  {

  PrintValue(5 + 3);

  return 0;

  }

  In this case, the expression 5 + 3 is evaluated to produce the result 8, which is placed in an anonymous variable. A copy of this anonymous variable is then passed to the PrintValue() function, which prints the value 8.

  Note how much cleaner this keeps our code — we don’t have to litter the code with temporary variables that are only used once.

  Anonymous class objects

  Although our prior examples have been with built-in data types, it is possible to construct anonymous objects of our own class types as well. This is done by creating objects like normal, but omitting the variable name.

  1

  2Cents cCents(5); // normal variable

  Cents(7); // anonymous variable

  In the above code, Cents(7) will create an anonymous Cents object, initialize it with the value 7, and then destroy it. In this context, that isn’t going to do us much good. So let’s take a look at an example where it can be put to good use:

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  18

  19

  20

  21

  22

  23

  24

  25

  26class Cents

  {

  private:

  int m_nCents;

  public:

  Cents(int nCents) { m_nCents = nCents; }

  int GetCents() { return m_nCents; }

  };

  Cents Add(Cents &c1, Cents &c2)

  {

  Cents cTemp(c1.GetCents() + c2.GetCents());

  return cTemp;

  }

  int main()

  {

  Cents cCents1(6);

  Cents cCents2(8);

  Cents cCentsSum = Add(cCents1, cCents2);

  std::cout << "I have " << cCentsSum.GetCents() << " cents." << std::endl;

  return 0;

  }

  Note that this example is very similar to the prior one using integers. In this case, our Add() function is constructing a short-lived cTemp variable that only serves as a placeholder. We are also using a cCentsSum variable in main().

  We can simplify this program by using anonymous variables:

  1

 

  24class Cents

  {

  private:

  int m_nCents;

  public:

  Cents(int nCents) { m_nCents = nCents; }

  int GetCents() { return m_nCents; }

  };

  Cents Add(Cents &c1, Cents &c2)

  {

  return Cents(c1.GetCents() + c2.GetCents());

  }

  int main()

  {

  Cents cCents1(6);

  Cents cCents2(8);

  std::cout << "I have " << Add(cCents1, cCents2).GetCents() << " cents." << std::endl;

  return 0;

  }

  This version of Add() functions identically to the one above, except it uses an anonymous Cents value instead of a named variable. Also note that in main(), we no longer use a named cCentsSum variable as temporary storage. Instead, we use the return value of Add() anonymously!

  As a result, our program is shorter, cleaner, and generally easier to follow (once you understand the concept).

  In C++, anonymous variables are primarily used either to pass or return values without having to create lots of temporary variables to do so. However, it is worth noting that anonymous objects can only be passed or returned by value! If a variable is passed or returned by reference or address, a named variable must be used instead. It is also worth noting that because anonymous variables have expression scope, if you need to reference a value in multiple expressions, you will have to use a named variable.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章