- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我无法理解如何对构造函数使用动态分配。
我在我的代码中使用了一个名为 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/
我想在我的单元测试中模拟一个遗留对象。这是构造函数: public Class LegacyClass{ public LegacyClass(Object... obj) {
此处说明https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function函数对象实例的构造函数属性“指定创建对
有没有办法从子类中的构造函数分配在父类(super class)中声明的实例变量?我已经习惯使用 BUILD() 作为构造函数,但我想知道这是否是个好主意。 IE: use v6; clas
鉴于以下情况: type AStruct struct { m_Map map[int]bool } 在这种情况下,AStruct的实例在AStruct.m_Map初始化之前不能使用: m_M
我是 Android 新手,我正在尝试学习如何使用 Gson 解析 API 调用。我已经阅读了一些内容,我正在尝试遵循这个示例:http://www.javacodegeeks.com/2011/01
我正在阅读 this文章,我不知道下面这行是做什么的。即使我删除了这一行,我也看不出有什么不同。 this.constructor.prototype.constructor.apply(this,A
这个问题已经有答案了: JsonMappingException: No suitable constructor found for type [simple type, class ]: can
我正在处理我的第一个 Flutter 项目,我正在构建一个登录页面,我创建了一个变量来存储一个 TextFormFieldController,但我收到了上面的错误,因为我删除了构造函数。当我返回这个
假设我们有以下主要和次要构造函数: open class Animal(val name:String){ internal constructor(message:InputStream): t
为什么默认复制构造函数不调用 monster 的基构造函数,但是当我在 troll 中包含一个用户定义的复制构造函数时,它会调用父级(即: 怪物) 构造函数? 我认为它的工作原理如下:创建基础对象,然
这个问题在这里已经有了答案: Is there a difference between foo(void) and foo() in C++ or C? (4 个答案) 关闭 8 年前。 我注意到
我将 T4MVC 与 MVC2 一起使用。 我有以下构建 block : 一个简单的实体接口(interface),它定义了每个 POCO 实体必须有一个 long Id属性(property): p
以下代码返回一个错误: “构造函数调用必须是构造函数中的第一个语句。” 我不明白。我的代码中的构造函数是第一条语句。我究竟做错了什么? public class labelsAndIcons exte
我是 kotlin 的新手,对它包含的所有有用的语法糖和功能感到惊讶。 但是每当我声明一个构造函数时,我都必须独立地将我的所有字段设为私有(private)。 class Result(private
作为主题,相关代码为: #include class ABC { public: ABC() { std::cout<< "default con
在 Haxe 中,我创建了一个名为 的类。我的类 喜欢: class MyClass { var score: String; public function new (score:
不确定为什么会这样,尝试删除所有 new 实例,从 const ect 切换到 let。可以运行站点,但是当我通过 html 表单运行发布请求时,在“const user = new UserSche
我是 Javascript 的新手,我正在尝试深入挖掘并理解继承、构造函数和原型(prototype)链。所以,我创建了一个构造函数, var a = function(){this.integer=
我知道 JavaScript 中的函数有双重生命,第一个是函数(作为创建实例的第一类事物),第二个是普通对象。 但是我很惊讶地看到下面控制台的输出。 function A() { consol
这个问题在这里已经有了答案: Why can't I access a property of an integer with a single dot? (5 个答案) 关闭 5 年前。 为什么
我是一名优秀的程序员,十分优秀!