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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:22:50+00:00 2026-05-12T06:22:50+00:00

I’m dealing specifically with C++, but this is really language-agnostic. Just to give some

  • 0

I’m dealing specifically with C++, but this is really language-agnostic.

Just to give some context into the problem… I have a tree/graph based structure where each node holds a collection of multiple items.

I have a class which encapsulates some generic collection (list, map, vector, LinkedList, whatever). I want to be able to fetch all the items from that collection in the most efficient way possible without the client being able to edit the private collection.

Here is a simplified example of what I have:

class MyClass
{
public:
    // Basic constructors and such
    void AddItem(int item) { _myItems->push_back(item); }

private:
    list<int>* _myItems;
};

So obviously a getter which retrieves the pointer to _myItems does not work as this will allow the client to edit _myItems.

Attempt 1:
I could create a new list and return a pointer to that one instead… however I don’t like that idea as the responsibility of creating and deleting now lie with different objects.

Attempt 2:
I’m thinking it would be best to create some CopyTo methods such as:

void CopyItemsToList(list<int>* inList) { // copy the items to client list }

This way, the client takes care of mem management and it is easy to extend this to allow more data structures. My concern with this approach is cost… The list can be very big, and the cost of copying the items could get large

Attempt 3:
Instead of keeping a pointer to a list in the class, just use a value type and return that (letting the copy constructor take care of it). But, this seems like the performance cost will be the same as #2…

Anyways, any other way to go about this? Let me know what you guys think… Thanks.

  • 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-12T06:22:51+00:00Added an answer on May 12, 2026 at 6:22 am

    The Iterator pattern is generally considered a proper method to expose a list of elements from a collection agnostic to the actual internal representation of the collection. You could create your iterator in such a way that it returns read-only (const) references, or make copies of items on the fly as they’re requested instead of copying the entire list.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.