这段代码是如何工作的?

发布于 2024-10-04 10:11:46 字数 1490 浏览 3 评论 0原文

我正在为傻瓜看 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 技术交流群。

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

发布评论

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

评论(5

冰葑 2024-10-11 10:11:46

++ 是自增运算符:它首先读取值(并将其分配给变量),然后仅然后递增它。

将此与预增量运算符进行对比:

value = ++nextStudentId;

这将具有您期望的行为。

查看此问题以获取更多信息: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:

value = ++nextStudentId;

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?

つ可否回来 2024-10-11 10:11:46
value = nextStudentId++;

这使用了所谓的后递增运算符。执行 nextStudentId++ 将首先使用 nextStudentId 的当前值作为 value,然后然后递增它。因此,第一轮,value 将为 1000,nextStudentId 随后将立即变为 1001,依此类推。

如果您改为执行:

value = ++nextStudentId;

这是预增量运算符,您将看到您之前所期望的内容。

value = nextStudentId++;

This uses what is called the post-increment operator. Doing nextStudentId++ will first use the current value of nextStudentId for value and afterwards increment it. So the first time round, value will be 1000 and nextStudentId will immediately afterwards become 1001, and so on.

If you instead do:

value = ++nextStudentId;

which is the pre-increment operator, you will see what you were expecting earlier.

饮惑 2024-10-11 10:11:46

在 C 和 C++ 中,递增(和递减)运算符以两种形式出现

  1. 前缀 - ++对象
  2. 后缀 - Object++

前缀形式递增,然后返回值,而后缀形式获取值的快照,递增对象,然后返回先前获取的快照。

T& T::operator ++(); // This is for prefix
T& T::operator ++(int); // This is for suffix

请注意,第二个运算符有一个虚拟 int 参数,这是为了确保这些运算符具有不同的签名。

您可以覆盖这些运算符,但请确保在覆盖的实现中维护语义以避免混淆。

In C and C++ increment (and decrement) operators occurs in two forms

  1. prefix - ++Object
  2. sufix - Object++

The prefix form increments and then return value, however suffix form takes snapshot of value, increment the object and then returns snapshot previously taken.

T& T::operator ++(); // This is for prefix
T& T::operator ++(int); // This is for suffix

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.

捶死心动 2024-10-11 10:11:46

试试这个

value = ++nextStudentId;

Try this

value = ++nextStudentId;
兲鉂ぱ嘚淚 2024-10-11 10:11:46

你知道运算符“x++”和“++x”如何工作吗?

“x++”首先返回x的值,然后将x递增

例如:

int x = 1;
std::cout << x++ << std::endl; // "1" printed, now x is 2
std::cout << x << std::endl; // "2" printed

“++x”首先将x递增,然后返回递增的值

int y = 1;
std::cout << ++y << std::endl; // "2" printed, y is 2
std::cout << y << std::endl; // "2"

Do you know how operators "x++" and "++x" works?

"x++" firstly returns value of x, and then it increments the x

for example:

int x = 1;
std::cout << x++ << std::endl; // "1" printed, now x is 2
std::cout << x << std::endl; // "2" printed

"++x" firstly increments the x, and then it returns incremented value

int y = 1;
std::cout << ++y << std::endl; // "2" printed, y is 2
std::cout << y << std::endl; // "2"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文