-2

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 ?

6
  • 3
    There is no way to do this, and you misunderstand the meaning of const. const only means that the function won't modify any non-mutable member variables, not that it will "have the same output" every time you call it. For instance, what if you had a static variable in a const member function and incremented it on every call and returned it? It would still be const but return a different value every time. Feb 4, 2013 at 18:36
  • @SethCarnegie Exactly. I asked the question because there does not seem to be a way to differentiate "constant" static member functions from "non-constant" static member functions, where the "constant" is with respect to the static member variables.
    – cmo
    Feb 4, 2013 at 18:41
  • @SethCarnegie And no, I did not fail to understand the meaning of const. I actually demonstrated my understanding in the question. On the contrary, you evidently failed to read my question, as your "comment" merely proposed merely another example of what my question is asking.
    – cmo
    Feb 4, 2013 at 18:43
  • 2
    @MatthewParks no, text that you edited out implied that const shouldn't change anything in the class, which is wrong. That's what I was referring to when I said you misunderstood const. Feb 4, 2013 at 19:15
  • 2
    @Cheersandhth.-Alf I have proved rigorously that there is no way to declare bar to be const with respect to a static variable by noting that the standard doesn't provide such a declaration. Whether or not the effect can be simulated is irrelevant to the fact that there is no such declaration. Feb 4, 2013 at 19:18

2 Answers 2

3

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 ) {}
};
2

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

No. C++ has not provided such a mechanism.

This deeply bothers me because one would assume that multiple calls to const member function foo would not change anything in the class

You should not assume that. A const method may modify static variables, because those variables are not part of the instance that is being declared const.

It could also modify variables marked mutable. Or members that have had their const-ness removed with const_cast.

And const functions can observe or modify variables outside of the class, so you should never consider a const function to behave the same way every time it's called. You could have a const function return rand() for example.

See this question to better understand the meaning of const in C++

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.