这段代码是如何工作的?
我正在为傻瓜看 c++,发现这段代码
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int nextStudentId = 1000; // first legal Student ID
class StudentId
{
public:
StudentId()
{
value = nextStudentId++;
cout << "Take next student id " << value << endl;
}
// int constructor allows user to assign id
StudentId(int id)
{
value = id;
cout << "Assign student id " << value << endl;
}
protected:
int value;
};
class Student
{
public:
Student(const char* pName)
{
cout << "constructing Student " << pName << endl;
name = pName;
semesterHours = 0;
gpa = 0.0;
}
protected:
string name;
int semesterHours;
float gpa;
StudentId id;
};
int main(int argcs, char* pArgs[])
{
// create a couple of students
Student s1("Chester");
Student s2("Trude");
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}
是输出
取下一个学生 ID 1000
构建学生切斯特
取下一个学生 ID 1001
构建学生特鲁德
按任意键继续。 。 .
我很难理解为什么第一个学生 ID 是 1000,如果 value 加一然后打印出来,
这
在 StudentId 的构造函数中是否有意义,这两行代码采用 nextStudentId
并向其添加 1,然后将其打印出来,
输出不是这样的吗:
取下一个学生 ID 1001
构建学生切斯特
取下一个学生 ID 1002
构建学生特鲁德
按任意键继续。 。 .
希望你明白我想说的
谢谢卢克
,
I am looking at c++ for dummies and found this code
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int nextStudentId = 1000; // first legal Student ID
class StudentId
{
public:
StudentId()
{
value = nextStudentId++;
cout << "Take next student id " << value << endl;
}
// int constructor allows user to assign id
StudentId(int id)
{
value = id;
cout << "Assign student id " << value << endl;
}
protected:
int value;
};
class Student
{
public:
Student(const char* pName)
{
cout << "constructing Student " << pName << endl;
name = pName;
semesterHours = 0;
gpa = 0.0;
}
protected:
string name;
int semesterHours;
float gpa;
StudentId id;
};
int main(int argcs, char* pArgs[])
{
// create a couple of students
Student s1("Chester");
Student s2("Trude");
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}
this is the output
Take next student id 1000
constructing Student Chester
Take next student id 1001
constructing Student Trude
Press any key to continue . . .
I am having a really hard time of understanding why the first student id is 1000 if value adds one to it and then prints it out
does this make sense
right in the constructor for studentId the two lines of code take nextStudentId
and add one to it then it gets printed out
wouldn't the output be something like this:
Take next student id 1001
constructing Student Chester
Take next student id 1002
constructing Student Trude
Press any key to continue . . .
hope you get what I am trying to say
thanks
Luke
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
++ 是后自增运算符:它首先读取值(并将其分配给变量),然后仅然后递增它。
将此与预增量运算符进行对比:
这将具有您期望的行为。
查看此问题以获取更多信息:Incrementing in C++ -何时使用 x++ 或 ++x?
The ++ is the post-increment operator: it first reads the value (and assigns it to a variable), and only then increments it.
Contrast this with the pre-increment operator:
This would have the behavior you expect.
Take a look at this question for more information: Incrementing in C++ - When to use x++ or ++x?
这使用了所谓的后递增运算符。执行
nextStudentId++
将首先使用nextStudentId
的当前值作为value
,然后然后递增它。因此,第一轮,value
将为 1000,nextStudentId
随后将立即变为 1001,依此类推。如果您改为执行:
这是预增量运算符,您将看到您之前所期望的内容。
This uses what is called the post-increment operator. Doing
nextStudentId++
will first use the current value ofnextStudentId
forvalue
and afterwards increment it. So the first time round,value
will be 1000 andnextStudentId
will immediately afterwards become 1001, and so on.If you instead do:
which is the pre-increment operator, you will see what you were expecting earlier.
在 C 和 C++ 中,递增(和递减)运算符以两种形式出现
前缀形式递增,然后返回值,而后缀形式获取值的快照,递增对象,然后返回先前获取的快照。
请注意,第二个运算符有一个虚拟 int 参数,这是为了确保这些运算符具有不同的签名。
您可以覆盖这些运算符,但请确保在覆盖的实现中维护语义以避免混淆。
In C and C++ increment (and decrement) operators occurs in two forms
The prefix form increments and then return value, however suffix form takes snapshot of value, increment the object and then returns snapshot previously taken.
Please note second operator has a dummy int parameter, this is to ensure different signatures for these operators.
You can override these operator, but its make sure to maintain the semantics in overridden implementation to avoid confusion.
试试这个
Try this
你知道运算符“x++”和“++x”如何工作吗?
“x++”首先返回x的值,然后将x递增
例如:
“++x”首先将x递增,然后返回递增的值
Do you know how operators "x++" and "++x" works?
"x++" firstly returns value of x, and then it increments the x
for example:
"++x" firstly increments the x, and then it returns incremented value