When the expression

本文探讨了C++中匿名变量的使用方法及其优势,包括简化代码、减少临时变量的使用等。通过实例展示了如何在函数参数和返回值中使用匿名变量,以及在自定义类对象中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值