C ++对象向量与向对象的指针向量 [英] C++ vector of objects vs. vector of pointers to objects

查看:189
本文介绍了C ++对象向量与向对象的指针向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用openFrameworks编写一个应用程序,但我的问题不是专门针对oF;

I am writing an application using openFrameworks, but my question is not specific to just oF; rather, it is a general question about C++ vectors in general.

我想创建一个包含另一个类的多个实例的类,但也提供了一个直观的界面来进行交互与那些对象。在内部,我的类使用的类的一个向量,但是当我试图使用vector.at()操作一个对象,程序将编译,但不能正常工作(在我的情况下,它不会显示视频)。

I wanted to create a class that contains multiple instances of another class, but also provides an intuitive interface for interacting with those objects. Internally, my class used a vector of the class, but when I tried to manipulate an object using vector.at(), the program would compile but not work properly (in my case, it would not display a video).

// instantiate object dynamically, do something, then append to vector
vector<ofVideoPlayer> videos;
ofVideoPlayer *video = new ofVideoPlayer;
video->loadMovie(filename);
videos.push_back(*video);

// access object in vector and do something; compiles but does not work properly
// without going into specific openFrameworks details, the problem was that the video would
// not draw to screen
videos.at(0)->draw();

在某处,建议我做一个指向该类的对象的向量,而不是一个向量的对象本身。

Somewhere, it was suggested that I make a vector of pointers to objects of that class instead of a vector of those objects themselves. I implemented this and indeed it worked like a charm.

vector<ofVideoPlayer*> videos;
ofVideoPlayer * video = new ofVideoPlayer;
video->loadMovie(filename);
videos.push_back(video);
// now dereference pointer to object and call draw
videos.at(0)->draw();

我为动态分配对象的内存,即 ofVideoPlayer = new ofVideoPlayer;

I was allocating memory for the objects dynamically, i.e. ofVideoPlayer = new ofVideoPlayer;

我的问题很简单:为什么使用指针的向量工作,以及何时创建一个对象的向量对这些对象?

My question is simple: why did using a vector of pointers work, and when would you create a vector of objects versus a vector of pointers to those objects?

推荐答案

对于c ++中的向量,你必须知道的是,他们必须使用类的copy操作符的对象,以便能够将它们输入到向量中。如果你在这些对象中的内存分配在析构函数被调用时自动去分配,这可以解释你的问题:你的对象被复制到向量然后销毁。

What you have to know about vectors in c++ is that they have to use the copy operator of the class of your objects to be able to enter them into the vector. If you had memory allocation in these objects that was automatically desallocated when the destructor was called, that could explain your problems : your object was copied into the vector then destroyed.

如果你在你的对象类中有一个指针指向一个分配的缓冲区,这个对象的副本将指向同一个缓冲区(如果你使用默认复制操作符)。如果析构函数去分配缓冲区,当调用复制析构函数时,原始缓冲区将被解除分配,因此您的数据将不再可用。

If you have, in your object class, a pointer that points towards a buffer allocated, a copy of this object will point towards the same buffer (if you use the default copy operator). If the destructor desallocates the buffer, when the copy destructor will be called, the original buffer will be desallocated, therefore your data won't be available anymore.

如果你使用指针,因为你通过new / destroy控制元素的生命,而向量函数只复制指向你的元素。

This problem doesn't happen if you use pointers, because you control the life of your elements via new/destroy, and the vector functions only copy pointer towards your elements.

这篇关于C ++对象向量与向对象的指针向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆