Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6105725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:00:07+00:00 2026-05-23T14:00:07+00:00

I am writing an application using openFrameworks, but my question is not specific to

  • 0

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.

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();

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:00:08+00:00Added an answer on May 23, 2026 at 2:00 pm

    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?

    std::vector is like a raw array allocated with new and reallocated when you try to push in more elements than its current size.

    So, if it contains A pointers, it’s like if you were manipulating an array of A*.
    When it needs to resize (you push_back() an element while it’s already filled to its current capacity), it will create another A* array and copy in the array of A* from the previous vector.

    If it contains A objects, then it’s like you were manipulating an array of A, so A should be default-constructible if there are automatic reallocations occuring. In this case, the whole A objects get copied too in another array.

    See the difference? The A objects in std::vector<A> can change address if you do some manipulations that requires the resizing of the internal array. That’s where most problems with containing objects in std::vector comes from.

    A way to use std::vector without having such problems is to allocate a large enough array from the start. The keyword here is “capacity”. The std::vector capacity is the real size of the memory buffer in which it will put the objects. So, to setup the capacity, you have two choices:

    1) size your std::vector on construction to build all the object from the start , with maximum number of objects – that will call constructors of each objects.

    2) once the std::vector is constructed (but has nothing in it), use its reserve() function : the vector will then allocate a large enough buffer (you provide the maximum size of the vector). The vector will set the capacity. If you push_back() objects in this vector or resize() under the limit of the size you’ve provided in the reserve() call, it will never reallocate the internal buffer and your objects will not change location in memory, making pointers to those objects always valid (some assertions to check that change of capacity never occurs is an excellent practice).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an application using labview and need to use external code. I
I'm writing a web application using Spring/Hibernate that displays a report to a user,
I am writing a web application using PHP. I want to use the MVC
I'm writing a XUL application using JavaScript for the coding. I would like to
I am writing an application in Java using Swing. I am trying to implement
I've been writing writing a small pygtk application using glade to put together the
I'm writing a Java Swing application using the Metal look-and-feel. Every time there is
I'm writing a GUI for an application using Swing, and in the interests of
I'm writing a GUI application in Python using wxPython and I want to display
I am writing an application in Java for the desktop using the Eclipse SWT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.