简体   繁体   English

初始化并存储在另一个类中的对象是否应该动态/静态分配?

[英]Should the objects initialized and stored in another class be dynamically/statically allocated?

I am new to C++ but I have some basic memory allocation knowledge in C. I am writing a class Card, which stores the card number and a list of class Activity object. 我是C ++的新手,但是我对C有一些基本的内存分配知识。我正在编写一个Card类,其中存储了卡号和Activity对象类的列表。

class Card {
    public:
    Card();
    ~Card();
    vector<Activity> activities;
    int cardNo;
}

Currently, I initialize the Activity object using code like: 目前,我使用以下代码初始化Activity对象:

Activity a = Activity("a"); 

and push them to the vector defined in the Card object. 并将其推入Card对象中定义的向量。

But I found people tend to initialize using Activity *a = new Activity("a") instead (dynamically allocation?), and the objects declared in the former way (statically allocated?) will be freed when the function declares them terminated. 但是我发现人们倾向于使用Activity * a = new Activity(“ a”)进行初始化(动态分配?),并且当函数声明它们终止时,以前一种方式声明的对象(静态分配?)将被释放。

Then, if I initialize Activity objects the same way I did before, but initialize Card using the "new Card()" way, is it possible that the Activity objects may have been de-allocated before Card object freed? 然后,如果以与以前相同的方式初始化Activity对象,但使用“ new Card()”方式初始化Card,那么是否有可能在释放Card对象之前取消分配了Activity对象? Should I switch to use "new Activity()" to initialize objects stored in Card? 我应该切换到“ new Activity()”来初始化Card中存储的对象吗?

No, what you're doing is fine. 不,你在做什么很好。 When you push an object onto a vector , a copy is made. 当您将一个对象推到vector ,将进行复制。 So when your function returns, your a is destroyed, but the vector you added it to still has its own seperate copy. 因此,当函数返回时, a被销毁,但是添加到其中的vector仍然具有其自己的单独副本。

One reason someone might allocate an instance of a class dynamically and push it onto a vector would be that copying objects of that particular class around is expensive (and vector does a lot of copying around internally) and they want to avoid that, so they store pointers instead of objects so that only copies of the pointers are made, not of the objects (which is would not be nearly so expensive). 有人可能会动态分配一个类的实例并将其推到向量上的一个原因是,复制该特定类的对象很昂贵( vector在内部进行了大量复制),并且他们希望避免这种情况,因此他们存储指针而不是对象,这样就只创建了指针的副本,而不是对象的副本(这几乎不会那么昂贵)。 That all depends on the class though; 但这全都取决于阶级。 generally you can use vectors of objects without any performance issues. 通常,您可以使用对象向量,而不会出现任何性能问题。

Note: a shortcut 1 for Activity a = Activity("a"); 注意: Activity a = Activity("a");的快捷方式1 Activity a = Activity("a"); is Activity a("a") , or better, do what Benjamin suggested and do activites.push_back(Activity("a")) if you're not performing some operations on the Activity before you push it. Activity a("a") ,或者更好,执行本杰明的建议并执行activites.push_back(Activity("a"))如果您未在推动该Activity之前对其执行任何操作。

1 It's not really a shortcut because it does something different, but for your intents and purposes, it is. 1它并不是真正的捷径,因为它可以做一些不同的事情,但是出于您的意图和目的,它确实是。

"But I found people tend to initialize using Activity *a = new Activity("a") instead (dynamically allocation?)" “但是我发现人们倾向于使用Activity * a = new Activity(“ a”)进行初始化(动态分配?)”

What people? 什么人 They're doing it wrong. 他们做错了。 You're doing it right, sort of. 您做对了,有点。 You could just do this instead: 您可以改为这样做:

activities.push_back(Activity("a"));

A few cases where you need pointers: 一些需要指针的情况:

  • it might be NULL instead of some dummy state 它可能为NULL而不是某些虚拟状态
  • it is polymorphic 它是多态的
  • shared, not exclusive to the class 共享的,不是课堂上专有的
  • there is a circular dependency or recursion that prevents a direct member variable 循环依赖或递归阻止直接成员变量

In this particular case, as with most STL containers, member variables are preferred over member pointers. 在这种特殊情况下,与大多数STL容器一样,成员变量优于成员指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动态分配的对象是否默认初始化? - Are dynamically allocated objects default initialized? 指向静态分配对象的指针 - Pointers to statically allocated objects 动态分配对象中的字段 - 更好的动态或静态分配? C++ - Fields within dynamically allocated objects - better dynamically or statically allocated? C++ 对静态分配的子类对象进行静态分配的纯虚拟父类引用是否合法? - Is it legal to have statically-allocated pure-virtual-parent-class references to statically-allocated child class objects? 如何删除在另一个 function 中初始化的动态分配数组? - How do I delete a dynamically allocated array that is initialized in another function? 使用静态分配的派生 class 对象来初始化基本 class 指针数组 - Using statically allocated derived class objects to initialize an array of base class pointers 动态或静态分配复杂数据成员更好? - Better for complex data members to be dynamically or statically allocated? 如何在向量中存储指向静态分配对象的指针? - How to store pointers to statically allocated objects in a vector? -&gt;运算符不允许用于C ++中的静态分配对象吗? - -> operator not allowed on statically allocated objects in C++? C++ 访问静态分配对象的成员 - C++ access statically allocated objects' members
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM