在下面的程序中。爲什麼函數在沒有C++對象的情況下被訪問?
#include<iostream>
using namespace std;
class Base{
public:
Base(){
cout<<"I am Constructor"<<endl;
}
void method();
};
void Base::method(){
cout<<"I am method"<<endl;
}
int main()
{
Base *sc1;
Base *sc2;
sc1->method();
sc2->method();
}
我得到的輸出如下
I am method I am method
這怎麼能發生,因爲沒有創建對象?
我喜歡你提到它可能「有效」,因爲沒有成員數據被訪問。在編輯你的作品之前即將寫下一個例子。我已經投了你的答案,所以你將不得不想像我的獎勵點! :) – Dennis