由 new 运算符创建的类实例与在 C++ 中使用其构造函数创建的类实例有什么区别[重复]

问题描述 投票:0回答:1

我不知道

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
有什么区别?

c++ pointers instance new-operator
1个回答
-2
投票
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;

}
© www.soinside.com 2019 - 2024. All rights reserved.