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

  • SEARCH
  • Home
  • 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 8369301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:36:27+00:00 2026-06-09T13:36:27+00:00

I’ve a program which creates many instances of my own Class. Then I operate

  • 0

I’ve a program which creates many instances of my own Class. Then I operate them: read, change, pass them to some third party methods etc.

There are three questions.

  1. Do I store an vector of instances or a vector of pointers? Why?

  2. If I do not want to change an instance do I pass to the function an instances or pointers to it? Why?

  3. If I do want to change an instance, do I pass a pointer or a reference? Why?

Thanks in advance!

Class example:

class Player {
    public:
        static const char               width = 35;
        static const char               height = 5;
        static const char               speed = 15;

        int                             socketFD;
        float                           xMin;
        float                           xMax;

        char                            status;
        float                           x;
        float                           y;
        char                            direction;

                                        Player ( int );
        void                            Reset();
        void                            Move();
        void                            SetHost();
        void                            SetClient();

    private:
        void                            EscapeCheck();
};
  • 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-09T13:36:28+00:00Added an answer on June 9, 2026 at 1:36 pm

    Do I store an array of instances or an array of pointers? Why?

    In case your object is big (i.e. more than 2-3 primitive types) you’d want to store a array of pointers to dynamically allocated instances. This will make reordering objects and resizing/reallocating the container much faster. If you are making use of polymorphism, you pretty much have to use pointers (note that references are also polymorphic) to the base class. This will prevent slicing, since all elements of an array must be the same size, so if you want to store multiple objects, pointers to the base class are the way to go.

    The cases where you might prefer storing the instances in the container is if you have a single type (no inheritance and polymorphism) and it is not that big (i.e. a few primitive member types). For bigger objects you might want to use a linked list instead of an array, in order to avoid costly inserts and reallocations.

    If i do not want to change an instance do I pass to the function an
    instances or pointers to it? Why?

    Pass a pointer to constant object or constant reference. Unless you are passing a primitive, where you can simply pass a cheap copy and avoid the modification of the original object.
    Whether it is a pointer to const object or a const reference is up to your usage scenario, references are easier to work with, like with normal instances, but cannot be changed to reference to other objects as pointers can.

    If i do want to change an instance, do i pass a pointer or a
    reference? Why?

    If you intend to change the instance, then pass either a pointer or a reference. Which one of the two – see above. This way you can directly modify the particular instance. You can also pass by value, but it will create one temporary copy, which you will later need to assign back to the original object. There is no point in doing that, and if modification is your intent, always go for pointers or references. Even if you don’t intend to modify an object, it is still much cheaper to pass a constant pointer or reference, since those are the size of an int, whereas the object might be significantly bigger, so there is no point copying it. If you are passing a primitive type, you might as well pass by value, although if you intend to modify the object, you still have one extra assignment which is avoidable.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.