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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:41:42+00:00 2026-05-26T02:41:42+00:00

If I have a definition in a class header such as this: vector<baddie*> baddies;

  • 0

If I have a definition in a class header such as this:

vector<baddie*> baddies;

which I then initialise in the constructor like this:

Class::Class(vector<baddie*> input)
{
    baddies = input;
}

What do I end up with? Two vectors with two sets of pointers pointing to the objects?

Would it be better to simply point to the original vector? Is this even possible?

Or would it be better to hold a vector of pointer references in the class so as to not duplicate pointers? What is the best practice for accessing objects and arrays of objects in multiple classes? References? Pointers? Pointer references? Thank you in advance!

  • 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-26T02:41:42+00:00Added an answer on May 26, 2026 at 2:41 am

    It depends on the semantics that you want to provide. In C++11 what you would probably want to do is something like:

    Class::Class( vector<baddie*> input ) : baddies( std::move(input) ) {}
    

    Which will move the memory from the copy in the argument to the member, while in C++03 you would probably write:

    Class::Class( vector<baddie*> const & input ) : baddies( input ) {}
    

    Which will initialize the vector with a copy.

    Note that this discussion only relates to the vector contents, not to the data pointed by those baddie pointers. That is, in both cases, there will be two vectors with pointers to refer to the same elements (i.e. only one copy of each baddie in memory, with two pointers referring to it).

    Depending on the semantics of your application, you might want to go from this intermediate shallow copying to either end: Perform a deep copy (i.e. create new baddie elements in memory, so that the original and the copy are fully unrelated once the constructor completes) or avoid coping at all, and just store a reference/pointer so that both vectors are exactly the same (insertion inside or outside of the class will be visible outside/inside the class).

    Also, beware of vector of pointers, you need to manage the memory manually before the vector is destructed.

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

Sidebar

Related Questions

Basically I have code which looks like this inside a header file: class Bar;
I have this definition in a header file: class Owner { private: // Fields
Let's say I have an object who's class definition looks like: class Command: foo
I have this definition of the function in my class. The .hpp file: class
I have a class set up like this: class Foo { Foo(); private: Bar
i have shared the header file containing class definition of a Context Free grammar
With C++, I can have one class definition in a header file, and have
I have the following class definition, written in C++, residing in it's own header
I have a c++ header file containing a class. I want to use this
I have all the class definitions in a header file: ModelModule.h. I have provided

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.