简体   繁体   English

这段代码是如何工作的?

[英]How does this code work?

I am looking at c++ for dummies and found this code 我正在寻找c ++ for dummies并找到了这段代码

#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 下一个学生身份1000

constructing Student Chester 建立学生切斯特

Take next student id 1001 下一个学生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 我很难理解为什么第一个学生ID是1000,如果值加一个然后将其打印出来

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 在studentId的构造函数中,两行代码接受nextStudentId并添加一行然后将其打印出来

wouldn't the output be something like this: 输出不会是这样的:

Take next student id 1001 下一个学生ID 1001

constructing Student Chester 建立学生切斯特

Take next student id 1002 下一个学生ID 1002

constructing Student Trude 建立学生特鲁德

Press any key to continue . 按任意键继续 。 . .

hope you get what I am trying to say 希望你得到我想说的话

thanks 谢谢

Luke 卢克

The ++ is the post -increment operator: it first reads the value (and assigns it to a variable), and only then increments it. 该++是 -Increment操作者:它首先读取值(并将其分配给一个变量), 才把它递增。

Contrast this with the pre -increment operator: 将此与pre -increment运算符进行对比:

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? 请查看此问题以获取更多信息: C ++中的增量 - 何时使用x ++或++ x?

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. 这样做nextStudentId++将首先使用的当前值nextStudentIdvalue之后加一。 So the first time round, value will be 1000 and nextStudentId will immediately afterwards become 1001, and so on. 所以第一次, value将为1000, nextStudentId将立即成为1001,依此类推。

If you instead do: 如果您改为:

value = ++nextStudentId;

which is the pre-increment operator, you will see what you were expecting earlier. 这是预增量运算符,您将看到之前的预期。

In C and C++ increment (and decrement) operators occurs in two forms 在C和C ++中,增量(和减量)运算符以两种形式出现

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

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. 请注意第二个运算符有一个伪int参数,这是为了确保这些运算符的不同签名。

You can override these operator, but its make sure to maintain the semantics in overridden implementation to avoid confusion. 您可以覆盖这些运算符,但它确保在重写的实现中维护语义以避免混淆。

尝试这个

value = ++nextStudentId;

Do you know how operators "x++" and "++x" works? 你知道运算符“x ++”和“++ x”是如何工作的吗?

"x++" firstly returns value of x, and then it increments the x “x ++”首先返回x的值,然后递增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 “++ x”首先递增x,然后返回递增的值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM