我正在测试这段代码并想知道为什么在编译时没有失败?我正在使用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)