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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:44:41+00:00 2026-05-22T20:44:41+00:00

Lets say that I have a class Dictionary { vector<string> words; void addWord(string word)//adds

  • 0

Lets say that I have a

class Dictionary
{
vector<string> words;  
void addWord(string word)//adds to words
{
/...
}
bool contains(string word)//only reads from words
{
//...
}
}

Is there a way to make compiler check that contains isnt changing words vector. Ofc this is just an example with one class data member, I would like it to work with any number of data members.
P.S. I know i have no public: and private:, I left it out intentionally to make code shorter and problem clearer.

  • 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-22T20:44:42+00:00Added an answer on May 22, 2026 at 8:44 pm

    If you want the compiler to enforce this, then declare the member function const:

    bool contains(string word) const
    {
        ...
    }
    

    A const function is not allowed to modify its member variables, and can only call other const member functions (either its own, or those of its member variables).

    The exception to this rule is if the member variable is declared as mutable. [But mutable should not be used as a general-purpose const workaround; it’s only really meant for when situations where the “observable” state of an object should be const, but internal implementation (such as reference-counting or lazy evaluation) still needs to change.]

    Note also that const does not propagate through e.g. pointers.

    So in summary:

    class Thingy
    {
    public:
        void apple() const;
        void banana();
    };
    
    class Blah
    {
    private:
        Thingy t;
        int *p;
        mutable int a;
    
    public:
        Blah() { p = new int; *p = 5; }
        ~Blah() { delete p; }
    
        void bar() const {}
        void baz() {}
    
        void foo() const
        {
            p = new int;  // INVALID: p is const in this context
            *p = 10;      // VALID: *p isn't const
    
            baz();        // INVALID: baz() is not declared const
            bar();        // VALID: bar() is declared const
    
            t.banana();   // INVALID: Thingy::banana() is not declared const
            t.apple();    // VALID: Thingy::apple() is declared const
    
            a = 42;       // VALID: a is declared mutable
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that I have the following code: public class Shelter<A extends Animal, B
lets say i have a background worker in a class that perform db query
Lets say I have a class that is supposed to generate some ID (for
So for starters lets say that I have a LinkedList<String> , I can easily
Lets say that i have a class with some instance variables and i want
Lets say that I have the two following classes public class OtherClass { public
Lets say I have a module called device which contains a class called ConfigureDevice
Let's say that I have class , that uses some functionality of dict .
Let's say that I have a class Foo: public class Foo { public static
Let's say that I have a bunch of class instances that serve different purposes,

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.