- 論壇徽章:
- 0
|
本來我認(rèn)為當(dāng)對一個(gè)使用new分配的變量進(jìn)行delete后,該變量就應(yīng)當(dāng)立即失效了.但使用以下的代碼作測試,結(jié)果卻不是我所預(yù)期的.
- #include <iostream>;
- using namespace std;
- class Cat
- {
- public:
- Cat()
- {
- cout << "\n Cat Constructs\n";
- }
- ~Cat()
- {
- cout << "\n Cat Destructs\n";
- }
- void sleep()
- {
- cout << "\nI'm sleeping\n";
- }
- void speek()
- {
- cout << "\nMEOW!\n";
- }
- void eat()
- {
- cout << "\nWhere is the mouse!\n";
- }
- };
-
- int main()
- {
- Cat* cat = new Cat();
- if (NULL == cat)
- return -1;
- delete cat;
- cat->;eat(); //Works;
- return (0);
- }
復(fù)制代碼
輸出:
Cat Constructs
Cat Destructs
Where is the mouse!
有兄弟能幫我解釋一下嗎? |
|