Hi,
As I'm sure C++ people here know, according to the C++ standard, a member function of a class can be made an inspector (ie guarenteed not to change data belonging to the class) by qualifying with const.
As a synthetic example:
-edit-
Forgot to put "static" before getInstanceCount() when hurridly posting example :lol:
-/edit-
[size=x-small]
class Dummy {
private:
[b]static[/b] int instances; // current number of Dummy objects
int value;
//..snip..
public:
// inspector methods
[b]static[/b] int getInstanceCount() [b]const[/b] { return instances; }
int getValue() [b]const[/b] { return value; }
//..snip..
Dummy() { instances++; }
~Dummy() { instances--; }
};
[/size]
In my old code (compiled under that champion of ANSI C++ conformity StormC v3 (:lol:)), I qualified inspection methods as const and it all worked fine.
Now, I'm moving all my old code to GCC/C++ with ANSI C++ compiler model. It's been keeping me off the streets and out of trouble and all...
I just stumbled upon something quite odd. In the above example, the getInstanceCount() cannot be qualified const.
I just did some quick checks of the literature and basically it said that any normal member function (constructors and destructors aside) of a class can be const qualified to make it explicit that it doesn't have rights to change the representation.
However, an equally brisk google showed various developer forums where this problem cropped up all over using GCC.
Is there some fundamental reason for this that I'm missing or what?