C ++中的动态对象数组 [英] Dynamic array of objects in C++

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

问题描述

我试图创建一个动态对象数组,类似于Java中的ArrayLists。我是C ++新手,无法使用它。

I'm trying to create a dynamic array of objects, similar to ArrayLists in Java. I'm new at C++ and can't get it to work.

class Album{

private:

public:
    void addPhoto(Photo p){

    }

};

我的私人数据构件应该是什么样子,如何添加 p 到它?

What should my private datamember look like and how do I add p to it? And do I need a pointer for some reason?

推荐答案

您寻找的功能已经退出 stl集合类,并知道你的应用程序,它将不得不告诉你天气你需要一个指针或不。

The functionality you look for already exits in the stl collection classes and and with out knowing you application it would be had to tell you weather you needed a pointer or not.

底层容器的基本布局可能是这样的。

The basic layout of you underlying container could be something like this.

class Album{    

public:
    void addPhoto(Photo p){
         Photos.push_back(p); 
    }
private:
    std::vector<Photo> Photos; 

};

这篇关于C ++中的动态对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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