c – 此代码如何工作?
我正在寻找傻瓜的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,如果值加一个然后将其打印出来 这有道理吗 在studentId的构造函数中,两行代码接受nextStudentId并添加一行然后将其打印出来 输出不会是这样的:
希望你得到我想说的话 谢谢 卢克 解决方法
它是后增量运算符:它首先读取值(并将其赋值给变量),然后才增加它.
将此与预增量运算符进行对比: value = ++nextStudentId; 这会有你期望的行为. 请查看此问题以获取更多信息:Incrementing in C++ – When to use x++ or ++x? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ruby-on-rails-3 – Rails 3:.group()按created
- react-native ios-run各种报错*错误*cfbundleide
- Cordova应用程序无法使用Xcode 7.3存档(未找到Co
- JSONObject和JSONArray对象的创建方法
- ruby-on-rails – Ruby on Rails map.root似乎没
- ruby-on-rails – 什么可能导致这个rails ioerro
- ajax在什么情况下会走success和error
- C++ STL array容器和普通数组的比较
- iphone – 类扩展中变量属性的自动合成
- ruby-on-rails – 行动之前的Rails跳过不起作用