Problem with static and inline in C++
I have class with a counter declared as static and I defined an assessor on the variables
The following code works:
Code:
class {
static int counter;
...
static int getCounter () {return counter;}
};
The method defined in the header of a class are inline by default. You can do this by defining the methods "inline" in the *.h but outside the class.
But the following code does not work:
Code:
class {
static int counter;
...
};
inline static int getCounter () {return counter;}
I have the impression that the compiler makes an error because it can not distinguish that it is a static method of a method that has a static return type.
Someone to a solution, please?
Re: Problem with static and inline in C++
The method is not "a static return type."
Your code does not work, because your function is not part of the class, and tries to access the counter, a member variable, without specifying the class name before.
In short it is the issue of accessing classing member outside the class without declaring its object.
Re: Problem with static and inline in C++
As said before, static does not need to be again, when defining the method:
Code:
class MyClass {
static int counter;
inline static int getCounter ();
};
int MyClass: getCounter () {return counter;}
Here the point is that the compiler is not obliged to follow the keyword 'inline' (unless you force the compiler to do so).
Re: Problem with static and inline in C++
Quote:
Originally Posted by
kelfro
Here the point is that the compiler is not obliged to follow the keyword 'inline' (unless you force the compiler to do so).
You mean the compiler is not forced to use the inline function call. The keyword "inline" is not used by it. It is used to perform several definitions of the same symbol in several translation units.