Welcome, Guest. Please login or register.

Author Topic: More GNU C++ (ANSI C++ mode) oddness  (Read 2911 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
More GNU C++ (ANSI C++ mode) oddness
« on: March 26, 2004, 03:25:33 PM »
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-

Code: [Select]
[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?
int p; // A
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #1 on: March 26, 2004, 04:07:42 PM »
@The Jackal

-edit-

Actally, I need this particular method to be static (my fault, I missed the static kewyord in the edxample - fixed now). Simply defining it a normal member that returns the static value is no good, since the use since the function is meant to be called without needing an object, eg:

printf("Num instances of Dummy : %d\n", Dummy::getInstanceCount());

-/edit-

It seems it doesn't like the notion that a static member function can be const qualified when there is no obvious reason for this that I can see.

Naturally a static member function would only be able to access static data belonging to the class, but that's no reason why it should be forbidden from becoming a pure inspector of that static data.
int p; // A
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #2 on: March 26, 2004, 04:09:23 PM »
@PiR

It does the same thing regardless of whether the function is defined inline or not.

I only made the Dummy example inline to save space in the post :-D
int p; // A
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #3 on: March 26, 2004, 04:18:26 PM »
@All

Sorry, I missed the static qualifier for the getInstanceCount() method in the example I posted.

Of course, I meant

static int getInstanceCount() const { return instances; }

It is the above combination of static and const qalification that GNU is not happy with...
int p; // A
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #4 on: March 26, 2004, 05:01:32 PM »
Bleh,

It's discrepencies like this give C++ a bad name from java coders :lol:

I don't think this is neccesarily an ANSI issue (unless you know for sure it is) more of a quality of implementation issue with GNU.

Oh well, I can live without the static inspectors being explicitly const qualified. It's not like I use that many static member functions ;-)
int p; // A
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #5 on: March 26, 2004, 05:38:23 PM »
Thanks.

I appreciate the argument, but can you ask your guy what his view on a class inspector method is? That is, a static function that explicitly inspects static data belonging to the class, as opposed to inspecting data belonging to an instance of the class.

I'm pretty curious as to the reasoning ;-)
int p; // A
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #6 on: March 26, 2004, 05:48:43 PM »
Quote

TheJackal wrote:
My answer would be that a static member function can only access the static member varables of that class, not of any instances of that class.


Granted. But that is no reason to assume it should have any automatic right to always be allowed to modify any of those static members, is it?
int p; // A
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #7 on: March 26, 2004, 05:57:02 PM »
Exactly.

A static member could always change an instance of the class that it is given access to, and can always change non constant data belonging to the class itself, regardless of wether that should be allowed or not.

I can't help feeling the lack of const declaration for static member inspectors is a total oversight :lol:

-edit-

Programmers :-D Man, do we know how to party on friday night, or what?

:roflmao:
int p; // A