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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:03:09+00:00 2026-05-25T17:03:09+00:00

Consider the following class: class SomeInfo { private: std::vector<someObj *> _myVector; public: const std::vector<const

  • 0

Consider the following class:

class SomeInfo
{
private:
    std::vector<someObj *> _myVector;

public:
    const std::vector<const someObj*>& getInfoVector() const
    {
        return _myVector;
    }
};

When I try to compile with gcc 4.1.2, it gives me the following error:

error: invalid initialization of reference of type 
‘const std::vector<const someObj*, std::allocator<const someObj*> >&’ 
from expression of type 
‘const std::vector<someObj*, std::allocator<someObj*> >

If I remove the ‘const’ in front of ‘someObj *’, then it compiles, but I don’t want to return vector with non-constant pointers, because I don’t want the objects referenced by them to be changed from outside my SomeInfo class. What would you do in this situation?

  • 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-25T17:03:10+00:00Added an answer on May 25, 2026 at 5:03 pm

    What I would do in this situation is forget about returning a vector (or reference thereto). The caller can’t make any changes, so doesn’t need to see any particular container. Other answers explain why you can’t refer to your vector as if it were a vector of const someObj*. For that matter, if in future you change your class to use a deque instead of a vector you can’t return a reference to that as if it were a vector either. Since callers just need access to the elements, let’s give them that:

    const someObj *getInfoAt(size_t n) const {
        return _myVector.at(n);
    }
    
    const_info_iterator getInfoBegin() const {
        return _myVector.begin();
    }
    
    const_info_iterator getInfoEnd() const {
        return _myVector.end();
    }
    
    size_t getInfoSize() const {
        return _myVector.size();
    }
    
    // plus anything else they need.
    

    const_info_iterator is a typedef to a class that’s slightly annoying to write. It has to wrap a std::vector<someObj *>::const_iterator and const-ify the type of operator*. boost::transform_iterator might be useful, or it’s not all that bad to write from scratch. Bidirectional iterators do require the most operator overloads, though.

    Now, if the caller really wants a vector of const someObj*, then using my interface they can easily get a snapshot at any particular moment:

    std::vector<const someObj*> mycopy(myinfo.getInfoBegin(), myinfo.getInfoEnd());
    

    What they can’t have is a vector that continually reflects changes made to some other vector of a different type somewhere else — std::vector just doesn’t do that.

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

Sidebar

Related Questions

Consider the following : class A { public: int xx; A(const A& other) {
Consider the following code: class A { public: A& operator=( const A& ); const
Consider the following class: public class Event<T> { public delegate void Handler<t>(t msg); private
Consider the following: class A { public: const int c; // must not be
Consider the following class: public class MyIntSet { private List<int> _list = new List<int>();
Consider the following class. class mapping_items { public: mapping_items(){} void add(const mapping_item* item) {
Consider the following: class A { private: A() {} public: A(int x = 0)
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following class: class Something : ISomething { public void DoesSomething(int x) {
Consider the following class hierarchy: public class Foo { public string Name { get;

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.