私はこのコードをテストしていますが、なぜこれがコンパイル時に失敗しなかったのか疑問に思っています?. 私は 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;
}