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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:23:30+00:00 2026-06-13T00:23:30+00:00

Hy, I would like to ask a question that puzzles me. I’ve a class

  • 0

Hy, I would like to ask a question that puzzles me.

I’ve a class like this:

class A { 

private:
std::vector<Object*>* my_array_;

...

public
std::vector<Object*>& my_array(); // getter
void my_array(const std::vector<Object*>& other_array); // setter
};

I wanted to ask you, based on your experience, what is the correct way of implementing the setter and getter in a (possible) SAFE manner.

The first solution came to my mind is the following.

First, when I do implement the setter, I should:
A) check the input is not a referring to the data structure I already hold;
B) release the memory of ALL objects pointed by my_array_
C) copy each object pointed by other_array and add its copy to my_array_
D) finally end the function.

The getter may produce a copy of the inner array, just in case.

The questions are many:
– is this strategy overkilling?
– does it really avoid problems?
– somebody really uses it or are there better approaches?

I’ve tried to look for the answer to this question, but found nothing so particularly focused on this problem.


That of using smart pointers is a very good answer, i thank you both.. it seems I can not give “useful answer” to more than one so I apologize in advance. 🙂

From your answers however a new doubt has raised.
When i use a vector containing unique_ptr to objects, I will have to define a deep copy constructor. Is there a better way than using an iterator to copy each element in the vector of objects, given that now we are using smart pointers?

  • 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-13T00:23:31+00:00Added an answer on June 13, 2026 at 12:23 am

    I’d normally recommend not using a pointer to a vector as a member, but from your question it seems like it’s shared between multiple instances.

    That said, I’d go with:

    class A { 
    private:
       std::shared_ptr<std::vector<std::unique_ptr<Object> > > my_array_;
    
    public
       std::shared_ptr<std::vector<std::unique_ptr<Object> > >  my_array(); // getter
       void my_array(std::shared_ptr<std::vector<std::unique_ptr<Object> > >  other_array); // setter
    };
    

    No checks necessary, no memory management issues.

    If the inner Objects are also shared, use a std::shared_ptr instead of the std::unique_ptr.

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

Sidebar

Related Questions

this is my first question in here, and I would like to ask if
I know this is a weird question to ask, but I would like to
I would like to ask this question to those people who have an experience
I would like to ask a theoritical question. Assuming that an application is using
I would like to ask a question about triggers. Let's say that I have
I would like to ask this question, because I'm not sure if I got
I would like to ask such question, I have XML xsd`s, which generate beans
i would like to ask a question about @UsesJAXBContext annotation in jax-ws. I try
I would like to ask a question. Now i am training in iOS with
I would like to ask a question on theming in liferay. What would be

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.