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

The Archive Base Latest Questions

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

class SomeClass { public: void Render() const; private: mutable Cache m_some_cache; }; Is the

  • 0
class SomeClass {
  public:
    void Render() const;

  private:
     mutable Cache m_some_cache;
};

Is the class above const-correct? When can I safely say “This operation doesn’t change the internal state of an instance”?

In the above example SomeClass is something that renders stuff on the screen. It uses a cache (for example OpenGL buffer objects) to allow faster processing for further calls. So the only thing that changes internally is the cache object. I’m asking myself if a cache already belongs to an internal state of a renderer.

The example is very minimal, but in my real application this goes down a road with many classes, i.e. a lot of Render() calls are involved, and most of them do caching only. But some do also load resources through a resource loader — is the assumption here still right that a method may be const even if it queries a resource manager for loading a resource?

  • 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-23T22:20:56+00:00Added an answer on May 23, 2026 at 10:20 pm

    Think of it this way. This is perfectly valid:

    void Type::print_self () const {
        std::cout << *this << std::endl;
    }
    

    You aren’t modifying the object itself, so the use of the const qualifier is perfectly valid. This method is modifying std::cout, but that doesn’t count as far as the constness of Type::print_self() goes.

    That said, mutable Cache looks to me to be a contradiction in terms unless you are only using that Cache element for local storage within Render. If you are truly using it as a cache it seems a bit dubious to qualify this element as mutable. Use it as a cache (e.g., across calls to Render rather than within a call to Render) and you have lied to both the compiler and to the user of the class.

    Edit
    Per the comments made by the OP, the Render method truly is the graphical equivalent of print_self(). The ‘real’ state of the object (presumably not shown for the sake of constructing a minimal working example) presumably isn’t modified by rendering. Designating Render as a const method is the right thing to do. If the Cache data member reason for being is to serve as speed bump that avoids the cost of constructing and destructing it with each call to Render there is nothing wrong with qualifying that Cache member as mutable (which is needed so that Render can remain const).

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

Sidebar

Related Questions

Let's say I have some Java code: public class SomeClass { static { private
Let's say I have a class class SomeClass { public void methodA() {} public
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
I declared a private variable vector<SomeClass> theVector; someplace inside my SomeClass class. Why can't
Consider this case: public class SomeClass { public void someMethod() { new SomeInterface() {
Suppose I have a class that processes some data: class SomeClass { public: void
public class SomeClass { public void SomeMethod() { GridView gv = new GridView(this); gv.setOnClickListener(new
I have a class that looks like this public class SomeClass { public SomeChildClass[]
I have a class SomeClass, which can populate itself from a datarow in it's
Say I've got a class like this: class Test { int x; SomeClass s;

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.