這兩個實例化和方法調用類型有什麼區別?C++中的對象實例化方法
採取此代碼爲例如:
class Test
{
public:
Test(int nrInstance)
{
std::cout << "Class " << nrInstance << " instanced " << std::endl;
}
~Test() { }
int retornaValue()
{
return value;
}
private:
const int value = 10;
};
int main(int argc, char *argv[])
{
Test *test1 = new Test(1);
Test test2(2);
std::cout << test1->retornaValue() << std::endl;
std::cout << test2.retornaValue() << std::endl;
return 0;
}
從什麼IVE讀,使用所述第一方式中,變量是在堆上分配,並且第二,在堆棧中,但的arent兩者Main
範圍內,並在函數退出後解除分配?
此外,兩種示例中的調用方法都不相同,爲什麼?
感謝編輯@remyabel,這種方式更好,它避免了混淆。 – Leandragem 2014-11-05 00:02:11
你似乎對C++的理解很少。在問這樣的超級基本問題之前,你應該先做一些研究或讀一本書。這是C++ 101 – Falmarri 2014-11-05 00:02:59
那麼你是對的。我是C++的新手,但在繼續下一步之前,我試圖更好地理解這些主題。你會推薦一個很好的C++閱讀材料嗎? – Leandragem 2014-11-05 00:04:18