why and when should I use operator 'new'?解决办法
why and when should I use operator 'new'?
It is interesting when I found I never use 'new ' but all classes work well.
So, here comes the question, could anyone tell me why and when I should use operator 'new '?
------解决方案--------------------
就像你定义了int a变量,你不需要用new来申请空间一样,编译器为你分配了空间.但是当你定义int *a的时候,你需要用new申请空间.
Class A
{
}
A a; //不用要用new,编译器自动在栈中为A分配了空间.
A *a;
a = new A;//手动在堆中申请空间
It is interesting when I found I never use 'new ' but all classes work well.
So, here comes the question, could anyone tell me why and when I should use operator 'new '?
------解决方案--------------------
就像你定义了int a变量,你不需要用new来申请空间一样,编译器为你分配了空间.但是当你定义int *a的时候,你需要用new申请空间.
Class A
{
}
A a; //不用要用new,编译器自动在栈中为A分配了空间.
A *a;
a = new A;//手动在堆中申请空间