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
?