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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:39:54+00:00 2026-05-26T06:39:54+00:00

If I have a const vector defined in my class, how can I go

  • 0

If I have a const vector defined in my class, how can I go about sorting it?

Attempting to sort a const vector will give errors since I’m changing the contents of a const vector.

  • 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-26T06:39:54+00:00Added an answer on May 26, 2026 at 6:39 am

    You don’t. If you need to modify it… well then it shouldn’t be const. The two goals are in direct conflict with one another.

    Instead of asking for a solution to a problem that doesn’t make sense, tell us what you are actually trying to accomplish here. Are you trying to return a vector from a method that you don’t want the caller to be able to modify? In that case, create a getter method and return a const vector&

    #include <vector>
    
    class Foo
    {
    public:
        // clients can't change this vector directly
        const std::vector<int>& get_vector() const { return _vec; }
    
        // you can still create an interface that allows 
        // mutation of the vector in a safe way, or mutate
        // the vector internally.
        void push_back( int i ) { _vec.push_back( i ); }
    private:
        std::vector<int> _vec;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a private const int defined in a C# class. I want compilation
In C++ it's recommended to have const-correctness everywhere. But since in .Net world, the
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have defined a class in C++ which holds an array of scalars of
I have a class Message that has a std::string as a data member, defined
I have a VectorN class, and a Vector3 class inherited from VectorN (which can
I have a class here that is defined like this: struct USERFPOINT { POINTFLOAT
I have a CognitiveEntity class, defined this way: class CognitiveEntity : public Object {
I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
I have a class defined like this: template< class TBoundingBox > class BoundingBoxPlaneCalculator {

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.