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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:32:20+00:00 2026-05-23T11:32:20+00:00

vector<Foo*>& getVectorOfFoo(); I want to provide a list of my Foo objects to others.

  • 0
   vector<Foo*>&         getVectorOfFoo();

I want to provide a list of my Foo objects to others. Is this the best way to do it? I am returning a reference here and not copying correct?

The caller could (accidentally) modify this list right? Is there a way to avoid this possibility? Could I return const vector ? But they could always modify the Foo objects as well and not much I can do there. 10-20 different people will be writing code that use this list of Foo.

  • 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-23T11:32:20+00:00Added an answer on May 23, 2026 at 11:32 am

    First don’t return a list of pointers.
    That makes it doubly unclear about the allowed actions.

    Boost has a solution (as usual).
    Return a pointer container. This exposes the pointers as normal members.

    boost::ptr_vector<Foo> const&  getVectorOfFoo();
    

    Now the user can not alter the returned vector.

    Example:

    #include <boost/ptr_container/ptr_vector.hpp>
    
    class Foo
    {
        public:
            void plop()         {}
            void poop() const   {}
    };
    
    boost::ptr_vector<Foo> const&  getVectorOfFoo()
    {
        static boost::ptr_vector<Foo>  instance;  // Create and fill container with FOO objects.
        instance.push_back(new Foo);
        return instance;
    }
    
    int main()
    {
        boost::ptr_vector<Foo> const&  value = getVectorOfFoo();  
    
        value[0].plop();  // Fail. not a const method (comment out this line)
        value[0].poop();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using the std::for_each, class A; vector<A*> VectorOfAPointers; std::for_each(VectorOfAPointers.begin(), VectorOfAPointers.end(), std::mem_fun(&A::foo)); If we have
void foo() { struct Foo { .. }; std::vector<Foo> vec; // why is this
So given the following template functions with partial specialization template<typename T> void foo(vector<T> &in)
How would you serialize/deserialize this class using boost::serialization? #include <vector> struct Foo { struct
I want to fill std::vector (or some other STL container): class Foo { public:
I have the following class: Class L{ public: bool foo(vector<bool> & data); private: C**
I'm parsing a file and creating a vector of a vector of Foo objects.
I am not sure why this doesn't compile: std::vector< const Obj& > myVector; void
I have the following code: void foo() { vector<double> v(100,1); // line 1 //
std::vector<Foo> vec; Foo foo(...); assert(vec.size() == 0); vec.reserve(100); // I've reserved 100 elems vec[50]

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.