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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:01:06+00:00 2026-06-18T06:01:06+00:00

In C++, how do you declare a static member function of a class to

  • 0

In C++, how do you declare a static member function of a class to be const with respect to the static member variables of that class?

Consider the following simple example.

myclass.h:

class myclass
{
    myclass()
    {
        myint = 0;
    }

    ~myclass() { }

    int myint;
    static int my_static_int;

    void foo(const int yourint) const;
    static void bar(const int newint );
};

myclass.cpp:

myclass::my_static_int = 0;

void myclass::foo(const int yourint) const
{
  if (yourint <= myint + my_static_int)
     std::cerr << "yourint  <= myint + my_static_int";
  else
      std::cerr << "yourint  >  myint + my_static_int";

  bar( yourint );
}

void myclass:bar(const int newint)
{
  my_static_int = newint;
}

main.cpp

...
myclass A;
A.foo(4);
A.foo(4);
..

The output would be:

yourint  >  myint + my_static_int
yourint  <= myint + my_static_int

Clearly, bar can be used within const member functions to change the static member variables of the class and thus change the result of const member function foo.

Can you declare bar to be const with respect to my_static_int ?

  • 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-06-18T06:01:07+00:00Added an answer on June 18, 2026 at 6:01 am

    Original code (I only tidied up the formatting):

    class myclass
    {
        myclass()
        {
            myint = 0;
        } 
        ~myclass()
        {}
    
        int myint;
        static int my_static_int;
    
        void foo(const int yourint) const;
        static void bar(const int newint );
    };
    

    In this class everything is private, inaccessible, by default: it is unusable.

    That is one example why it is so important to post real code.

    You then ask:

    “Can you declare bar to be const with respect to my_static_int?”

    Yes, but there is no direct syntax for that.

    You will have to use the existing language features.

    They are adequate to the task.

    class something
    {
    private:
        int my_int;
    public:
        void bar( int const newint ) const;
        something();
    };
    
    class myclass
    {
    private:
        int myint;
        static something;
    public:
        void foo( int const yourint ) const;
        ~myclass() {}
        myclass(): myint( 0 ) {}
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to forward declare a static member function of a class in another
I can declare private static member variables in a class, but what does it
I have a class, in which i declare a static function. I register this
I have a static class data member declared as: static const float MINIMUM_ZOOM_FACTOR =
Possible Duplicate: C++ - Why static member function can’t be created with ‘const’ qualifier
I cannot figure the syntax to declare a function pointer as a static member.
suppose employee is a class....print is its non-static member function which prints the value
I have a class declaration(.h file) like so: struct MyClass { static const uint32_t
Are variables declared inside a static block accessible anywhere else? What kind of member
How do you declare an Objective-C static method that can be called without a

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.