Let's say I have a class FOO.
假设我有一个FOO类。
I want to have a std::vector
of FOO.
我想要一个FOO的std :: vector。
Is it better if I do something like this:
如果我做这样的事情会更好:
FOO foo;
foo.init();
foo.prop = 1;
std::vector<FOO> myvec;
myvec.push_back(foo);
foo.prop = 2;
myvect.push_back(foo);
or is it better practice to do:
或者更好的做法是:
std::vector<FOO> myvec;
FOO foo;
myvec.push_back(foo);
myvec.back().init();
myvec.back().prop = 1;
myvec.push_back(foo);
myvec.back().init();
myvec.back().prop = 2;
I'm basically not sure if its better to make a model and push in the model instead of making an instance, pushing it, then modifying it from the vector. Also, which one is safer and least likely to result in memory leaks?
我基本上不确定是否更好地制作模型并推入模型而不是创建实例,推送它,然后从向量中修改它。另外,哪一个更安全,最不可能导致内存泄漏?
Thanks
5 个解决方案
#1
8
Neither method has any memory issues as you're dealing with values and aren't dynamically allocating any objects manually.
在处理值时,这两种方法都没有任何内存问题,也没有手动动态分配任何对象。
I would favour giving FOO
a constructor which does whatever init
does and sets prop
to the appropriate value. Then you can just push the values you want:
我倾向于给FOO一个构造函数,它执行任何初始化并将prop设置为适当的值。然后你可以推送你想要的值:
myvec.push_back(FOO(1));
myvec.push_back(FOO(2));
#2
10
Best practice is not to have an init() function - you want a constructor. If you always need to set prop, give the constructor a parameter to do it. This has nothing specifically to do with vectors - it's the way that all C++ code should be written.
最佳做法是不要有init()函数 - 你想要一个构造函数。如果您总是需要设置prop,请为构造函数指定一个参数。这与向量没有任何关系 - 它是应该编写所有C ++代码的方式。
#3
1
I think the best is to:
我认为最好的是:
myvec.push_back(FOO(1));
myvec.push_back(FOO(2));
#4
1
The answer depends on what your FOO
class does. If it's a simple structure with no pointers etc., then both your approaches are fine and do the same.
答案取决于你的FOO课程的作用。如果它是一个没有指针等的简单结构,那么你的方法都很好并且做同样的事情。
Note that push_back
inserts a copy of the object into the vector. If your class allocates memory on the heap, you need a copy constructor that creates a deep copy of your objects, otherwise you'll end up with memory leaks. Also, if your objects are quite large, it may be inefficient to create copies. In such cases, what I generally do is allocate the object itself on the heap externally and insert the pointer into the vector:
请注意,push_back将对象的副本插入到向量中。如果您的类在堆上分配内存,则需要一个复制构造函数来创建对象的深层副本,否则最终会导致内存泄漏。此外,如果您的对象非常大,创建副本可能效率低下。在这种情况下,我通常做的是在外部在堆上分配对象本身并将指针插入向量:
std::vector<FOO *> myvec;
FOO *foo;
foo = new FOO();
foo->init();
foo->val = 1;
myvec.push_back(foo);
foo = new FOO();
foo->init();
foo->val = 2;
myvec.push_back(foo);
However, in this case, you need to remember to free the objects before destroying the vector.
但是,在这种情况下,您需要记住在销毁向量之前释放对象。
#5
0
Besides what others have said about initializing your objects in the constructor, I would add this:
除了其他人在构造函数中初始化对象的内容之外,我还要添加:
In your second example, where you put objects in the vector and then initialize them, you risk leaving your vector in an unusable state.
在第二个示例中,您将对象放在向量中然后初始化它们,您可能会将向量置于不可用状态。
If for example, the init() method can throw an exception, you'll have a non/partially initialized object in your vector.
例如,如果init()方法可以抛出异常,则向量中将包含非/部分初始化对象。
Of course, these problems goes away with a constructor that makes sure the objects are properly initialized.
当然,这些问题会随着构造函数而消失,该构造函数确保对象被正确初始化。
In general: Don't start doing stuff with objects before they're in a usable state.
一般情况下:在对象处于可用状态之前,不要开始使用对象。
#1
8
Neither method has any memory issues as you're dealing with values and aren't dynamically allocating any objects manually.
在处理值时,这两种方法都没有任何内存问题,也没有手动动态分配任何对象。
I would favour giving FOO
a constructor which does whatever init
does and sets prop
to the appropriate value. Then you can just push the values you want:
我倾向于给FOO一个构造函数,它执行任何初始化并将prop设置为适当的值。然后你可以推送你想要的值:
myvec.push_back(FOO(1));
myvec.push_back(FOO(2));
#2
10
Best practice is not to have an init() function - you want a constructor. If you always need to set prop, give the constructor a parameter to do it. This has nothing specifically to do with vectors - it's the way that all C++ code should be written.
最佳做法是不要有init()函数 - 你想要一个构造函数。如果您总是需要设置prop,请为构造函数指定一个参数。这与向量没有任何关系 - 它是应该编写所有C ++代码的方式。
#3
1
I think the best is to:
我认为最好的是:
myvec.push_back(FOO(1));
myvec.push_back(FOO(2));
#4
1
The answer depends on what your FOO
class does. If it's a simple structure with no pointers etc., then both your approaches are fine and do the same.
答案取决于你的FOO课程的作用。如果它是一个没有指针等的简单结构,那么你的方法都很好并且做同样的事情。
Note that push_back
inserts a copy of the object into the vector. If your class allocates memory on the heap, you need a copy constructor that creates a deep copy of your objects, otherwise you'll end up with memory leaks. Also, if your objects are quite large, it may be inefficient to create copies. In such cases, what I generally do is allocate the object itself on the heap externally and insert the pointer into the vector:
请注意,push_back将对象的副本插入到向量中。如果您的类在堆上分配内存,则需要一个复制构造函数来创建对象的深层副本,否则最终会导致内存泄漏。此外,如果您的对象非常大,创建副本可能效率低下。在这种情况下,我通常做的是在外部在堆上分配对象本身并将指针插入向量:
std::vector<FOO *> myvec;
FOO *foo;
foo = new FOO();
foo->init();
foo->val = 1;
myvec.push_back(foo);
foo = new FOO();
foo->init();
foo->val = 2;
myvec.push_back(foo);
However, in this case, you need to remember to free the objects before destroying the vector.
但是,在这种情况下,您需要记住在销毁向量之前释放对象。
#5
0
Besides what others have said about initializing your objects in the constructor, I would add this:
除了其他人在构造函数中初始化对象的内容之外,我还要添加:
In your second example, where you put objects in the vector and then initialize them, you risk leaving your vector in an unusable state.
在第二个示例中,您将对象放在向量中然后初始化它们,您可能会将向量置于不可用状态。
If for example, the init() method can throw an exception, you'll have a non/partially initialized object in your vector.
例如,如果init()方法可以抛出异常,则向量中将包含非/部分初始化对象。
Of course, these problems goes away with a constructor that makes sure the objects are properly initialized.
当然,这些问题会随着构造函数而消失,该构造函数确保对象被正确初始化。
In general: Don't start doing stuff with objects before they're in a usable state.
一般情况下:在对象处于可用状态之前,不要开始使用对象。