在条件中声明两个变量?

发布于 2024-09-10 06:03:42 字数 217 浏览 2 评论 0原文

我可以在 C++ 的条件中声明两个变量吗?编译器给出了一个错误,但我仍然认为我应该得到一个意见:

int main()
{
    double d1 = 0;
    if((double d2 = d1) || (double d3 = 10))
        cout << "wow" << endl;
    return 0;
}

Can I declare two variables in a conditional in C++. The compiler gave an error but still I think I should get an opinion:

int main()
{
    double d1 = 0;
    if((double d2 = d1) || (double d3 = 10))
        cout << "wow" << endl;
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

悲凉≈ 2024-09-17 06:03:42

您只能对一个变量执行此操作

if(double d2 = d1)
    cout << "wow" << endl;
else 
if(double d3 = 10)
    cout << "wow" << endl;

,但我会在条件之外声明它们。如果您担心范围,您可以随时限制它:

{
  double d2 = d1;
  double d3 = 10;
  if(d2 || d3)
    cout << "wow" << endl;
}

当然,即使 d2 计算结果为 true,这也会计算第二个初始值设定项。但我认为这并不重要。

You can only do that for one variable

if(double d2 = d1)
    cout << "wow" << endl;
else 
if(double d3 = 10)
    cout << "wow" << endl;

But i would declare them outside of the conditions. If you are scared about the scope, you can always limit it:

{
  double d2 = d1;
  double d3 = 10;
  if(d2 || d3)
    cout << "wow" << endl;
}

Of course, this evaluates the second initializer even if d2 evaluates to true. But i assume that's not important.

蝶…霜飞 2024-09-17 06:03:42
if((double d2 = d1) || (double d3 = 10))
    cout << d2 + d3 << endl;

|| 运算符短路,因此 d2 + d3 表达式引用(可能)未初始化的 d3。这样的功能会产生很多不良影响,而且在我看来没有任何好处,所以这可能就是它不存在的原因。

if((double d2 = d1) || (double d3 = 10))
    cout << d2 + d3 << endl;

the || operator short-circuits, so the d2 + d3 expression references (potentially) uninitialized d3. such a feature would have many ill effects and IMO no benefit, so that's probably why it's not there.

孤蝉 2024-09-17 06:03:42

可能还有其他事情也困扰着您,但是 || 运算符的工作方式存在问题。我不记得标准中的具体措辞,但在 a || 中b 如果 a 计算结果为 true,则您的编译器不应该计算b,或者您的编译器可能选择 不要这样做。

考虑到这一点,在您的语句中您将不确定您的变量是否已声明 - 在 C++ 中您只能使用声明的变量,因此如果您询问,在您的示例中 d3 将是一个非常无用的变量我(因为你不确定它是否已被声明)。

There might be something else that is ALSO bothering you here, but there is a problem with how the ||-operator works. I don't remember exactly how it is worded in the standard, but in a || b either your compiler should not evaluate b if a evaluates to true, or your compiler may opt not to do so.

Considering that, in your statement you won't be sure if your variable has been declared - in C++ you can only used declared variables, so d3 will be a pretty useless variable in your example, if you ask me (as you won't be sure if it has been declared).

无法回应 2024-09-17 06:03:42

这在 C++ 中是不可能的,也许您正在考虑 C# 或更新的编程语言。不过,在条件之外声明 d2 和 d3 并没有什么缺点。

This is not possible in C++, maybe you were thinking of C# or a newer programming language. No disadvantages to declaring d2 and d3 outside the conditional though.

始终不够 2024-09-17 06:03:42

您可能想问 C++ 标准文档在哪里。

我不认为这是一个好主意,你想做的是告诉编译器:

声明 d2
将 d1 放入 d2 中
将 d2 与 0 进行比较
如果是 != 0 跳转到下面的代码
别的
声明 d3
将 10 放入 d3
将 d2 与 0 进行比较
如果是 != 0 跳转到下面的代码
别的
跳过下面的代码

仅在条件上方声明 d2 和 d3 可能是一个好主意,前提是您使用有限的内存(并且不希望在到达这段代码之前为 d2 和 d3 分配空间)。
这一切都归结为你真正想做的事情。

You may want to ask where the C++ standard documentation is.

I don't think that it's a good idea, what you are trying to do is to tell the compiler:

declare d2
put d1 in d2
compare d2 with 0
if it's != 0 jump to the code below
else
declare d3
put 10 in d3
compare d2 with 0
if it's != 0 jump to the code below
else
skip the code below

Just declaring d2 and d3 above the conditional could be a good idea, only if you work with limited memory (and do not wish to allocate room for d2 and d3 until this piece of code is reached).
It all boils down to what you really intend to do.

如梦初醒的夏天 2024-09-17 06:03:42

在 C++ 中,您可以执行以下操作:

if (bool b = (a == b)) {
  // Do something with b here...
}

或:

if (double d2 = d1) {
  // ...
}

但是,我从未见过在实际代码中使用这种样式。我认为总是有一种更清晰的方式来写同样的事情。

此外,不支持尝试在 ifwhile 语句的条件部分声明多个变量。您必须求助于嵌套的 ifelse if 才能获得相同的语义。

示例:

if (double d2 = d1) {
  cout << "wow" << endl;
} else if (double d3 = 10) {
  cout << "wow" << endl;
}

这样,仅当 d2 计算结果为 false 时才会创建 d3

In C++ you can do something like this:

if (bool b = (a == b)) {
  // Do something with b here...
}

or:

if (double d2 = d1) {
  // ...
}

However, I've never seen this style used in real code. I think there is always a clearer way to write the same thing.

Furthermore, trying to declare multiple variables in the conditional part of an if or while statement is not supported. You will have to resort to nested ifs or else if to get the same semantics.

Example:

if (double d2 = d1) {
  cout << "wow" << endl;
} else if (double d3 = 10) {
  cout << "wow" << endl;
}

This way d3 is only created if d2 evaluates to false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文