小编Lix*_*Bai的帖子

c ++中字符串的这两个不同初始化之间的区别是什么?

源代码

#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++ string initialization

5
推荐指数
1
解决办法
126
查看次数

如何理解此声明中的typedef

最近,我读了有效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;

c++ typedef forward-declaration effective-c++

4
推荐指数
4
解决办法
942
查看次数