小编Har*_*s N的帖子

c ++构造函数的奇怪行为

我正在测试这段代码并想知道为什么在编译时没有失败?我正在使用c ++ 11和g ++ 4.7.2.

我的生产代码上有类似的结构,它在运行时给出错误,然后我发现我正在用错误的参数类型构造类.

#include <iostream>
#include <vector>


typedef std::vector<std::string> Word;

class Data {
    public:
        const Word &word;
        Data(Word w) : word(w) {}
};

class Base{
    const Data &data;
    public:
        Base(const Data &d): data(d) {}
        ~Base() {}
};

class Work : public Base{
    public:
        Work(const Data &d): Base(d){}
        ~Work() {}
};


int main(int argc, char **argv){
    Word words;
    words.push_back("work");

    /*
    * I'm confused with this constructor, why this passed the compilation
    * ??
    * Any special rule to …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

5
推荐指数
2
解决办法
334
查看次数

标签 统计

c++ ×1

c++11 ×1