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
Luke
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?
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.
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.
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.
尝试这个
value = ++nextStudentId;
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"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.