我不知道
new
返回的指针中创建的实例和调用构造函数创建的实例有什么区别class Vec2{
public:
double x;
double y;
Vec2(double x, double y){
this->x = x;
this->y = y;
}
}
int main(){
Vec2* v = new Vec2(0,1);
Vec2 a = *v;
Vec2 b = Vec2(0,1);
return 0;
}
a
和b
有什么区别?
int main()
{
Vec2* v = new Vec2(0,1); // Create a new Vec2 on the heap.
Vec2 a = *v; // Create a Vec2 on the stack that is a copy of the one on the heap
Vec2 b = Vec2(0,1); // Create a Vec2 on the stack
return 0;
}