gpt4 book ai didi

C++ : operator new and default constructor

转载 作者:搜寻专家 更新时间:2023-10-31 01:46:15 25 4
gpt4 key购买 nike

我无法理解如何对构造函数使用动态分配。

我在我的代码中使用了一个名为 graph 的类(它只是一个 bool 二维矩阵,表示节点之间的边)和以下构造函数/析构函数(还有其他方法,但我认为它在这里不相关):

class graph{

private:
bool** edges;
int size;

public:
graph(int size = 0):size(size){
edges = new bool*[size];
for (int i = 0; i < size; i++){
edges[i] = new bool[size];
}
}

~graph(){
for(int i = 0; i < size; ++i) {
delete [] edges[i];
}
delete [] edges;
}

//others methods

};

在我的 main 中,我想使用动态分配:

int main()
{

int size;
cout << "Enter graph size :" << endl;
cin >> size;

graph g1 = new graph(size);

//some processing code

return 0;
}

但是,我在实例化时遇到错误(即 new graph(size) ):

从“graph*”到“int”的无效转换 [-fpermissive]

我真的不明白哪里出了问题,我很确定这是我在其他地方看到过的语法。

实际上,我真的不明白内存分配是如何与对象的创建一起工作的。

在这里,我在我的构造函数中使用 new 来创建 bool 二维矩阵,所以它会进入堆,不是吗?但是,如果我使用以下静态指令实例化对象: 图 g1(const_size);

那么它不应该进入堆栈吗?

预先感谢您的回答。

编辑

最后一个问题:

graph *g1 = new graph(size);

is storing g1 (pointer) on stack, but the object is created on the heap.

graph g1(size);

is creating object on the stack, and g1 is a reference to it.

但在任何情况下,矩阵边都会在堆上?或者在第二种情况下,它会以某种方式在堆栈上结束?

最佳答案

错误在这里:

graph g1 = new graph(size);

这应该是:

graph *g1 = new graph(size);

原因如下:

new graph(size)

正在创建新的 graph 对象并返回指向它的指针(使用 graph* 类型),并且:

graph g1 = ... 

正在尝试将其转换为对象 int(以便调用 graph(int) 构造函数) - 因此错误是 invalid conversion from 'graph *' 到 'int'

现在,我想这是某种练习,因为在此示例中您不应该在堆上使用分配。但是,如果您无论如何都要使用它,请永远不要忘记:

delete g1;

Actually, I dont really get how memory allocation works with the creation of object.

graph *g1 = new graph(size);

正在堆栈上存储 g1(指针),但对象是在堆上创建的。

graph g1(size);

正在堆栈上创建对象,g1 是对它的引用。

附言避免这种情况:

 graph g1 = graph(const_size);

这将首先创建语句右侧的评估,它将临时 graph 对象,并将使用复制构造函数初始化 g1

But for example, if I want a program who can deal with various graph size, without recompiling it each time I want to work with a different size, I would need something like that, no ?

不,您可以为此使用堆栈分配(我刚刚注意到在您的示例中,您使用的是 const_size - 在堆栈上分配的对象的构造函数的参数不需要是常量):

int size;
std::cout << "Enter size: ";
std::cin >> size;
graph g1(size);

Yes, dynamic allocation is not needed here, I just did that code to practice myself. But it would be necessary if I wanted to work with various sized graph without recompiling, no ?

不,请看上面的示例 - 堆栈分配的可变大小对象 - 无需重新编译。

I thought we were not supposed to call the destructor ourselves and that it was going to be called anyway when we go out of scope ? (I even read that it was actually bad to call it in some cases, as it might be calling it a second time)

在堆栈分配对象的情况下是正确的——析构函数将在作用域的末尾被调用。但是,如果您在堆上分配对象,则在您调用 delete 之前不会调用析构函数。

But in any case, the matrix edges would be on the heap ?

    edges = new bool*[size];
for (int i = 0; i < size; i++){
edges[i] = new bool[size];
}

是的,整个矩阵分配在堆上。

关于C++ : operator new and default constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20920177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com