我正在测试这段代码,想知道为什么在编译时没有失败?我正在使用 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 reason this scenario ??
*
* But obviously it will fail at run time.
*/
const Work *work = new Work(words);
return 0;
}