你认为下面这个程序会输出什么?
#include <iostream>
#include <string>
using namespace std;
string mostImportantOrders = "DO NOT LAUNCH THE MISSLES!";
void appendFour(int s) {
cout << s + "4" << endl;
}
int main() {
cout << "The total number of missiles is:" << endl;
appendFour(44);
cout << endl;
return 0;
}
输出结果。。却是。。
The total number of missiles is:
NOT LAUNCH THE MISSLES!
目前确实是没搞明白为什么会输出这样。。感觉和string构造对象机制有关,并且也和cout输出对象机制有关。
将上面的程序该为下面的程序:
#include <iostream>
#include <string>
using namespace std;
string mostImportantOrders = "DO NOT LAUNCH THE MISSLES!";
void appendFour(int s) {
cout << s + '4' << endl;
}
int main() {
cout << "The total number of missiles is:" << endl;
appendFour(44);
cout << endl;
return 0;
}
输出如下:
The total number of missiles is:
96
其中44+'4' = 44 + 4 + 48 = 96;