设计问题:使用malloc? [英] Design question: using malloc?
问题描述
你好,
我对C ++很陌生,并且有一些基础知识。所以我知道如何解决一个给定的问题,因为我有时会回到C.这就是我想要避免在当前项目中使用的
。因此我有以下问题
:
我维护一个创建的对象列表。有一些信息
与每个实例在逻辑上相关。我把它包装在一个
这样的结构中:
struct myListEntry {
myObject entry;
int relatedData;
}
实现向向量添加另一个对象的理想方法是什么?
(myVector有类型向量< myListEntry>在这个例子中)
我应该使用malloc为我的struct保留内存吗?
我应该执行以下操作吗?
...
myListEntry newEntry;
newEntry.entry = something;
newEntry.relatedData = something_else;
myVector.push_back(newEntry);
...
或者还有另一种首选方式吗?
问候,>
Christof Krueger
Hello,
I''m quite new to C++ and have some base C knowledge. So I know how to
solve a given problem, by I sometimes tend to fall back to C. That is
what I want to avoid in my current project. Therefore I have the
following question:
I maintain a list of objects that are created. There is some information
that is logically related to each instance. I encapulate this in a
struct like this:
struct myListEntry {
myObject entry;
int relatedData;
}
What is the ideal way to implement adding another object to the vector?
(myVector has type vector<myListEntry> in this example)
Should I use malloc to reserve memory for my struct?
Should I do the following?
...
myListEntry newEntry;
newEntry.entry = something;
newEntry.relatedData = something_else;
myVector.push_back(newEntry);
...
Or is there another preferred way?
Regards,
Christof Krueger
推荐答案
" Christof Krueger" <是ne ** @ pop2wap.net>在消息中写道
news:br ************* @ news.t-online.com ...
"Christof Krueger" <ne**@pop2wap.net> wrote in message
news:br*************@news.t-online.com...
你好,
我对C ++很陌生并且拥有一些基础知识。所以我知道如何解决一个给定的问题,我有时会回到C.这就是我想要在当前项目中避免的事情。因此我有以下问题:
我维护一个创建的对象列表。有一些与每个实例逻辑相关的信息。我在这样的结构中包装它:
struct myListEntry {
myObject entry;
int relatedData;
}
>实现向向量添加另一个对象的理想方法是什么?
(myVector在此示例中具有类型向量< myListEntry>)
我应该使用malloc为我的结构保留内存吗?
我应该执行以下操作吗?
...
myListEntry newEntry;
newEntry.entry = something;
newEntry.relatedData = something_else;
myVector.push_back(newEntry) ;
...
或者还有另一种首选方式吗?
问候,
Christof Krueger
Hello,
I''m quite new to C++ and have some base C knowledge. So I know how to
solve a given problem, by I sometimes tend to fall back to C. That is
what I want to avoid in my current project. Therefore I have the
following question:
I maintain a list of objects that are created. There is some information
that is logically related to each instance. I encapulate this in a
struct like this:
struct myListEntry {
myObject entry;
int relatedData;
}
What is the ideal way to implement adding another object to the vector?
(myVector has type vector<myListEntry> in this example)
Should I use malloc to reserve memory for my struct?
Should I do the following?
...
myListEntry newEntry;
newEntry.entry = something;
newEntry.relatedData = something_else;
myVector.push_back(newEntry);
...
Or is there another preferred way?
Regards,
Christof Krueger
std :: vector(和一般的STL容器)在内部分配内存,因为需要
。在这种情况下,您对''push_back''的调用会产生
的副本,该参数存储在向量中。 vector为
对象分配内存,并使用复制构造函数或赋值运算符来初始化它。
存储的对象在一个容器中必须至少是''可分配''
这通常意味着它有一个复制构造函数和一个赋值
运算符(如果它是一个简单的类型或这些简单类型的组合是
并不总是需要的。
如果你将这些添加到你的结构中:
struct myListEntry {
myListEntry(const myObject& obj,const int& n)
:entry(obj),relatedData(n){}
myListEntry& operator =(const myListEntry& rhs)
{
entry = rhs.entry;
relatedData = rhs.relatedData;
返回* this;
}
myObject条目;
int relatedData;
}
那么你提供的代码不仅会很好,而且你也可以写:
myVector.push_back(myListEntry(something,something_else));
Tom
std::vector (and STL containers in general) allocate memory internally as
required. In this instance, your call to ''push_back'' results in a copy of
the argument being stored in the vector. vector allocates memory for
the object and uses either a copy constructor or assignment operator to
initialize it.
An object stored in a container must at the minimum be ''Assignable''
which generally means that it has a copy constructor and an assignment
operator (if it is a simple type or a combination of simple types these are
not always required).
If you add these to your struct:
struct myListEntry {
myListEntry(const myObject& obj, const int& n)
: entry(obj), relatedData(n) {}
myListEntry & operator = (const myListEntry &rhs)
{
entry = rhs.entry;
relatedData = rhs.relatedData;
return *this;
}
myObject entry;
int relatedData;
}
then not only will the code you provided be fine but you could also write:
myVector.push_back(myListEntry(something, something_else));
Tom
>如果将这些添加到结构中:
> If you add these to your struct:
struct myListEntry {
myListEntry(const myObject& obj,const int& n)
:entry(obj),relatedData (n){}
myListEntry& operator =(const myListEntry& rhs)
{
entry = rhs.entry;
relatedData = rhs.relatedData;
return * this;
}
myObject entry;
int relatedData;
}
然后你提供的代码不仅会很好,而且你也可以写:
myVector。 push_back(myListEntry(something,something_else));
struct myListEntry {
myListEntry(const myObject& obj, const int& n)
: entry(obj), relatedData(n) {}
myListEntry & operator = (const myListEntry &rhs)
{
entry = rhs.entry;
relatedData = rhs.relatedData;
return *this;
}
myObject entry;
int relatedData;
}
then not only will the code you provided be fine but you could also write:
myVector.push_back(myListEntry(something, something_else));
我只需要检查3本书(没有结果),然后在网上搜索
详细了解C ++中struct和class之间的区别。似乎
唯一的区别是,在类中,默认可见性是私有的,而在结构中它是公共的。很高兴知道:)
非常感谢你的帮助,Thomas!
结论是:在C ++中处理malloc / free太多了(甚至在
全部?!?)并不是一个好主意,应该通过使用STL中的save
组件来避免。是吗?
再见,
Christof
I just had to check 3 books (without result) and then search the web to
find out the difference between struct and class in C++ in detail. Seems
that the only difference is that in a class the default visibility is
private and in a struct it is public. Good to know :)
Thank you very much for your help, Thomas!
The conclusion is: Dealing with malloc/free in C++ too much (or even at
all?!?) is not a good idea and should be avoided by using save
components from STL. Is that right?
Bye,
Christof
星期一,15 2003年12月00:48:29 +0100 comp.lang.c ++,Christof Krueger
< ne ** @ pop2wap.net>据称写了:
On Mon, 15 Dec 2003 00:48:29 +0100 in comp.lang.c++, Christof Krueger
<ne**@pop2wap.net> was alleged to have written:
我只需检查3本书(没有结果),然后在网上搜索,以便详细了解C ++中struct和class之间的区别。似乎
唯一的区别是,在类中,默认可见性是私有的,而在结构中它是公共的。很高兴知道:)
这个问题包含在问题中[7.8]
关键字struct和class之间有什么区别? ; Marshall Cline的C ++ FAQ。您可以在以下网址获取
常见问题解答: http: //www.parashift.com/cpp-faq-lite/
结论是:在C ++中处理malloc / free太多(甚至在
所有?! ?)不是一个好主意,应该通过使用STL中的save
组件来避免。是吗?
I just had to check 3 books (without result) and then search the web to
find out the difference between struct and class in C++ in detail. Seems
that the only difference is that in a class the default visibility is
private and in a struct it is public. Good to know :)
This issue is covered in question "[7.8] What''s the difference between the
keywords struct and class?" of Marshall Cline''s C++ FAQ. You can get the
FAQ at: http://www.parashift.com/cpp-faq-lite/
The conclusion is: Dealing with malloc/free in C++ too much (or even at
all?!?) is not a good idea and should be avoided by using save
components from STL. Is that right?
简答:是的。
Short answer: yes.
这篇关于设计问题:使用malloc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!