解释 C++ 中声明性谬误的最佳方式?

发布于 2024-08-03 01:24:05 字数 607 浏览 2 评论 0原文

由于作者试图以声明方式而不是过程方式编写 C++ 代码,因此如何才能很好地解释为什么以下代码不正确?

const double NEWTONS_PER_POUND = 4.448;

int main()
{
   double pounds, newtons;
   pounds = newtons/NEWTONS_PER_POUND; /* pounds equals 'unassigned variable'/4.448 */
   newtons = 10.0;
   cout << pounds << endl;             /* a big number, not 10.0/4.448 */
   return 0;
}

作者期望 cout 显示正确的计算结果,但得到的却是一个“疯狂的数字”。

我会将此解释为“C++ 是过程性的,因此在声明时

pounds = newtons/NEWTONS_PER_POUND;

newtons 尚未分配值。

有更好的建议吗?或者解释为什么 C++ 不够“智能”执行用户错误预期的行为?

How might one craft a good explanation of why the following code is not correct, in that the author is attempting to write C++ code declaratively rather than procedurally?

const double NEWTONS_PER_POUND = 4.448;

int main()
{
   double pounds, newtons;
   pounds = newtons/NEWTONS_PER_POUND; /* pounds equals 'unassigned variable'/4.448 */
   newtons = 10.0;
   cout << pounds << endl;             /* a big number, not 10.0/4.448 */
   return 0;
}

The author expected cout to display a proper calculation, but instead gets a "crazy number."

I would explain this as "C++ is procedural, and therefore at the time of the declaration

pounds = newtons/NEWTONS_PER_POUND;

newtons has not been assigned a value.

Any better suggestions? Or an explanation why C++ isn't "smart" enough to carry out the behavior the user mistakenly expected?

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

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

发布评论

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

评论(9

独自←快乐 2024-08-10 01:24:05

告诉作者

pounds = newtons/NEWTONS_PER_POUND;

命令CPU

  • 取“newtons”地址处的值
  • 取“NEWTONS_PER_POUND”地址处的值
  • 除以将
  • 结果存储在“pounds”地址处

他正在寻找for 很可能是命令式的函数:

double newtons_to_pounds(double newtons) {
  return newtons/NEWTONS_PER_POUND;
}

...

newtons = 10.0;
cout << newtons_to_pounds(newtons) << endl;    /* a big number, not 10.0/4.448 */
return 0;

Tell the author that

pounds = newtons/NEWTONS_PER_POUND;

commands the CPU to

  • take the value at the address referred to as "newtons"
  • take the value at the address referred to as "NEWTONS_PER_POUND"
  • divide them
  • store the result at the address referred to as "pounds"

what he is looking for is most probably a function in imperative terms:

double newtons_to_pounds(double newtons) {
  return newtons/NEWTONS_PER_POUND;
}

...

newtons = 10.0;
cout << newtons_to_pounds(newtons) << endl;    /* a big number, not 10.0/4.448 */
return 0;
淡看悲欢离合 2024-08-10 01:24:05

C++ 是一种命令式编程语言,而不是方程求解器。

C++ 按照编写语句的顺序执行语句。除非被告知,否则 C++ 不会初始化变量。 C++ 允许您使用值尚未初始化的变量,但是当您这样做时,结果是未指定。未指定意味着任何事情都可能发生,包括产生“疯狂的数字”之类的坏事。

详细解释如下:

double pounds, newtons;
pounds = newtons/NEWTONS_PER_POUND;
newtons = 10.0;

第一条语句声明了两个变量,但没有初始化它们。此时,它们的值尚未指定。

第二条语句读取 newtons 的值(可以是任何值)并将其除以 NEWTONS_PER_POUND。结果(可以是任何值)被分配给

第三条语句初始化了newtons,但是已经来不及影响我们刚刚执行的计算了。

C++ is an imperative programming language not an equation solver.

C++ executes statements in the order that you write them. C++ does not initialize variables unless it is told to. C++ allows you to use a variable whose value has not been initialized, but when you do this the result is unspecified. Unspecified means than anything can happen, including bad things like producing "crazy numbers".

Here's the detailed explanation:

double pounds, newtons;
pounds = newtons/NEWTONS_PER_POUND;
newtons = 10.0;

The first statement declares two variables without initializing them. At this point, their values are unspecified.

The second statement reads the value of newtons (which could be anything) and divides it by NEWTONS_PER_POUND. The result (which could be anything) is assigned to pounds.

The third statement initializes newtons, but it is too late to affect the calculation we just performed.

如梦 2024-08-10 01:24:05

好吧,无论学生的背景如何,解释起来都应该不难:只要告诉他们 C++ 一次一步地评估程序,一条语句接着一条语句(尽管存在诸如重新排序之类的编译器工件)。

C++ 处理这个问题的方式绝对没有什么特别的,它甚至不限于计算机编程——相反,它是处理有序指令列表的日常方式。

Well that shouldn’t be too hard to explain, regardless of the students’ background: just thell them that C++ evaluates programs one step at a time, statement after statement (notwithstanding compiler artifacts such as reordering …).

There’s absolutely nothing special to C++’ way of handling this, nor is it even limited to computer programming – rather, it’s an everyday way of dealing with an ordered list of instructions.

人生百味 2024-08-10 01:24:05

它不是懒惰地评估牛顿

,因此计算是在声明时执行的,而不是在请求时执行的。他追求的是功能代码,而不是 C++ 会做的事情。

It's not lazy evaluating newtons

As such the calculation is performed at the time of declaration, not at the time of request. He's after functional code, not what C++ will do.

原野 2024-08-10 01:24:05

如果这个人不太懂技术,你可以尝试:

“这个C++程序中的语句就像做蛋糕所需的步骤。你必须一步一步地执行这些步骤,并且必须按照一定的顺序执行它们才能完成。”取得成功。”

If the person is not overly technical you could try:

"The statements in this C++ program are like the steps required to make a cake. You must perform the steps one by one and they must be to be performed in a certain order for it to be a success."

弄潮 2024-08-10 01:24:05

解释一下磅是用赋值运算符赋值的:

pounds = newtons/NEWTONS_PER_POUND;

如果情况并非如此,但磅在使用时被评估(与 cout 语句一样),那么如果牛顿的值发生变化,则该值英镑也会发生变化。由于磅不是任何类型的指针,而是一个简单的整数,因此这是不可能的。

Explain that pounds is assigned a value on the line with the assignment operator:

pounds = newtons/NEWTONS_PER_POUND;

If this were not the case, but that pounds was evaluated when it was used (as with the cout statement), then if the value of newtons changed, then the value of pounds would change as well. Since pounds is not any type of pointer, but is a simple integer, this is not possible.

木有鱼丸 2024-08-10 01:24:05

在调试器中单步执行代码怎么样?

IME 没有什么比这更能理解用过程语言编写的程序的执行情况(即,模仿 CPU 实际执行代码的方式)。

What about stepping through the code in a debugger?

IME there's nothing like this to understand the execution of a program written in a procedural language (i.e., modeled on how the CPU actually executes code).

为你拒绝所有暧昧 2024-08-10 01:24:05

你试图让听众经历范式转变——改变他/她理解这段代码的整个方法。

“磅”只是一个数字。它没有
它是如何创建的概念。你告诉
“磅”是如何创建的,它不会
记住。它只会记住什么
它就是这样,而不是它是如何创建的。

将一块内存拟人化似乎有点奇怪。 :-)

You are trying to get the listener to undergo a paradigm shift - to change his/her entire approach to comprehending this code.

"pounds" is just a number. It's got no
concept of how its created. You tell
"pounds" how it's created, it won't
remember. It will just remember what
it is, not how it is created.

It may seem a bit strange to anthropomorphise a block of memory. :-)

萤火眠眠 2024-08-10 01:24:05

举一个稍微复杂一点的例子,其中像 newtons 这样的变量被重复使用并多次赋值。例如:

double pounds, newtons;

newtons = 10.0;
pounds = newtons/NEWTONS_PER_POUND;
cout << pounds << endl;

newtons = 15.0;
pounds = newtons/NEWTONS_PER_POUND;
cout << pounds << endl;

return 0;

向他展示代码和输出。然后请他解释程序如何以及为何为每一行生成不同的数字。我认为这应该有助于推动他将程序视为一个从上到下运行的过程。

Take a slightly more complex example where a variable like newtons is reused and assigned values more than once. For instance:

double pounds, newtons;

newtons = 10.0;
pounds = newtons/NEWTONS_PER_POUND;
cout << pounds << endl;

newtons = 15.0;
pounds = newtons/NEWTONS_PER_POUND;
cout << pounds << endl;

return 0;

Show him both the code and the output. Then ask him to explain how and why the program produces a different number for each line. I'd think that should help push him in the direction of viewing the program as a procedure that runs from top to bottom.

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