当用引用传参时候,内存会有错误
代码如下:
#include <iostream> using namespace std; class A { protected: int i; public: A(){ i = 99; } }; class B :public A { public: void BF(){cout << i << endl; }//内存错误 }; class C { private: B &b; public: C(B &b) :b(b)//********这里用应用传参***********运行会错误 { } void CF(){ b.BF(); } }; class D : public B, public C { public: D() : B(), C(*this) { CF(); } }; int main(int argc, char* argv[]) { D d; return 0; }
代码如下:
#include <iostream> using namespace std; class A { protected: int i; public: A(){ i = 99; } }; class B :public A { public: void BF(){cout << i << endl; } }; class C { private: B *b; public: C(B *b) :b(b)//********这里用引用传参***********运行会错误 { } void CF(){ b->BF(); } }; class D : public B, public C { public: D() : B(), C(this) { CF(); } }; int main(int argc, char* argv[]) { D d; return 0; }
原文:http://blog.csdn.net/ganze_12345/article/details/19237829