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 8232745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:55:56+00:00 2026-06-07T17:55:56+00:00

So, I have a class which stores a vector of pointers to objects. I

  • 0

So, I have a class which stores a vector of pointers to objects. I have a method that adds objects to the vector. When adding, I know I can pass by reference or by pointer, and have read about the advantages and disadvantages of each, but in this case, I can’t figure out which one is better and why. For all I can figure out, they’re pretty much the same (but I’m probably wrong!)

Here’s (a paraphrasing of) passing by pointer/address:

hpp:
class Room {
    vector<Item*> items;
public:
    void addItem(Item*);
};

cpp:
void Room :: addItem(Item* item) {
    items.push_back(item);
}

…and pass by reference:

hpp:
class Room {
    vector<Item*> items;
public:
    void addItem(Item &);
};

cpp:
void Room :: addItem(Item &item) {
    items.push_back(&item);
}

Which should I use?

  • 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-06-07T17:55:58+00:00Added an answer on June 7, 2026 at 5:55 pm

    It doesn’t matter in the slightest, so do whatever seems more natural at the call site, or is more consistent with the rest of the code.

    OK, I can see points in favour of each, but I can’t tell you if they matter in your case.

    • passing by pointer more accurately reflects how you’re using the argument (although I can’t tell whether that’s useful semantic info or an implementation detail)
      • a call site passing a pointer (or the result of new directly) will look natural
      • a call site doing something stupid like passing the address of an automatic variable will stand out
    • passing by reference suggests the argument should not be NULL, which may or may not be correct and useful
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector t_vec that stores references to instances of class Too .
I have a vector called actorVector which stores an array of objects of type
I have a class, user, which stores objects of another class, foo, in a
I have a class A, which has a static vector of objects. The objects
I have the following class which stores pairs of dates and prices: #include <vector>
I have a std::multiset which stores elements of class A . I have provided
At the moment inside the class I have ArrayList which the objects are stored
I have class which have one public method Start , one private method and
I have written a class (AbcdBase) which holds several static objects, these include maps
I have a a polygon class which stores a list of Microsoft.Xna.Framework.Vector2 as the

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.