源代码
#include <iostream>
#include <string>
using namespace std;
int main(){
std::string s{'a', 'b', '\0', 'c'};
std::string s1="ab\0c";
cout<<s.size()<<" "<<s<<endl;
cout<<s1.size()<<" "<<s1<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
而输出是
4 abc
2 ab
Run Code Online (Sandbox Code Playgroud)
我想知道为什么会出现这种现象,这两种类型的初始化在C++中有什么区别吗?谢谢.
最近,我读了有效C++这本书,第35项中有关于typedef的声明使我感到困惑.
class GameCharacter; // Question1: Why use forward declaration?
int defaultHealthCalc(const GameCharacter& gc);
class GameCharacter{
public:
typedef int (*HealthCalcFunc)(const GameCharacter&); // Question 2: What does this mean?
explicit GameCharacter(HealthCalcFunc hcf = defaultHealthCalc)
: healthFunc(hcf)
{}
int healthValue() const
{return healthFunc(*this); }
private:
HealthCalcFunc healthFunc;
};
Run Code Online (Sandbox Code Playgroud)
所以我的第一个问题是:为什么作者在这里使用前瞻性声明?有什么具体原因吗?
我的第二个问题是:我如何理解typedef声明,以及如何使用它?我只知道类似的东西typedef int MyInt;