如果我们在下面注释掉强调的行,我们会在控制台中获得777。 否则我们会得到一些垃圾,如(-534532345)。 我的环境是Microsoft Visual Studio 2012 Pro。跟踪对象和引用
class C
{
public:
C() { x = 777; }
void ou() {cout << x;}
protected:
int x;
};
class A
{
public:
A(C & rrc) : rc(rrc) {};
void koo() {rc.ou();}
protected:
C & rc;
};
int _tmain(int argc, _TCHAR* argv[])
{
C c;
C * pc = new C;
A a(*pc);
delete pc; // <<<< this line
a.koo();
return 0;
}
任何人都可以帮我弄清楚为什么我看到这种行为?
为什么这会构成语言中的“漏洞”? C++ *不是一种安全的语言*,并且不会像这样公布。 – 2013-02-27 23:10:33