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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:21:42+00:00 2026-05-14T04:21:42+00:00

I’m a bit confused about the object references. Please check the examples below: class

  • 0

I’m a bit confused about the object references. Please check the examples below:

class ListHandler {
public:
   ListHandler(vector<int> &list);
private:
   vector<int> list;
}

ListHandler::ListHandler(vector<int> &list) {
   this->list = list;
}

Because of the internal

vector<int> list;

definition, here I would be wasting memory right? So the right one would be:

class ListHandler {
public:
   ListHandler(vector<int>* list);
private:
   vector<int>* list;
}

ListHandler::ListHandler(vector<int>* list) {
   this->list = list;
}

ListHandler::~ListHandler() {
   delete list;
}

Basically all I want is to create a vector and pass to ListHandler. This vector will not be used anywhere else than the ListHandler itself so I’m expecting ListHandler to do all the other things and cleanup etc. stuff.

  • 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-14T04:21:42+00:00Added an answer on May 14, 2026 at 4:21 am

    It depends on whether you want to share the underyling vector or not. In general, I think it is a good practice to avoid sharing wherever possible, since it removes the question of object ownership. Without sharing:

    class ListHandler 
    {
        public:
            ListHandler(const std::vector<int>& list) : _list(list) {}
            ~ListHandler(){}
        private:
            std::vector<int> _list;
    };
    

    Note that, unlike in your example, I make it const since the original will not be modified. If, however, we want to hang on to and share the same underlying object, then we could use something like this:

    class ListHandler 
    {
        public:
            ListHandler(std::vector<int>& list) : _list(&list) {}
            ~ListHandler(){}
        private:
            std::vector<int>* _list;
    };
    

    Note that in this case, I choose to leave the caller as the owner of the object (so it is the caller’s responsiblity to ensure that the list is around for the lifetime of the list handler object and that the list is later deallocated). Your example, in which you take over the ownership is also a possibility:

    class ListHandler 
    {
        public:
            ListHandler(std::vector<int>* list) : _list(list) {}
            ListHandler(const ListHandler& o) : _list(new std::vector<int>(o._list)) {}
            ~ListHandler(){ delete _list; _list=0; }
    
            ListHandler& swap(ListHandler& o){ std::swap(_list,o._list); return *this; }
            ListHandler& operator=(const ListHandler& o){ ListHandler cpy(o); return swap(cpy); }
        private:
            std::vector<int>* _list;
    };
    

    While the above is certainly possible, I personally don’t like it… I find it confusing for an object that isn’t simply a smart pointer class to acquire ownership of a pointer to another object. If I were doing that, I would make it more explicit by wrapping the std::vector in a smart pointer container as in:

    class ListHandler 
    {
        public:
            ListHandler(const boost::shared_ptr< std::vector<int> >& list) : _list(list) {}
            ~ListHandler(){}
        private:
            boost::shared_ptr< std::vector<int> > _list;
    };
    

    I find the above much clearer in communicating the ownership. However, all these different ways of passing along the list are acceptable… just make sure users know who will own what.

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

Sidebar

Ask A Question

Stats

  • Questions 378k
  • Answers 378k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't have a direct answer, but for PayPal I… May 14, 2026 at 9:12 pm
  • Editorial Team
    Editorial Team added an answer Yes we can! Depending on the version of LLVM you… May 14, 2026 at 9:12 pm
  • Editorial Team
    Editorial Team added an answer smsniff from Nirsoft. May 14, 2026 at 9:12 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.