Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline TheJackal

  • Jr. Member
  • **
  • Join Date: Oct 2003
  • Posts: 95
    • Show all replies
Re: More GNU C++ (ANSI C++ mode) oddness
« on: March 26, 2004, 04:00:33 PM »
have you tried

Code: [Select]
[size=x-small]
class Dummy {
  private:
    static int instances; // current number of Dummy objects
    int value;
   
  //..snip..
  public:
    // inspector methods
    int getInstanceCount() const { return [color=FF0000]Dummy::[/color]instances; }
    int getValue() const { return value; }
  //..snip..
    Dummy() { [color=FF0000]Dummy::[/color]instances++; }
    ~Dummy() { [color=FF0000]Dummy::[/color]instances--; }
};
[/size]


Since the var is static and belongs to the class.
_________________
Any views, opinions, statements or advice in this message are solely those of the author and do not necessarily represent those of any organisation or individuals.

\\"Don\\\'t make me dance,.... You wouldn\\\'t like me when I dance.\\" - The Hulk
 

Offline TheJackal

  • Jr. Member
  • **
  • Join Date: Oct 2003
  • Posts: 95
    • Show all replies
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #1 on: March 26, 2004, 05:17:33 PM »
This also happens on msdev c++ compiler:

Code: [Select]

class Dummy
{  
private:    
   static int instances; // current number of Dummy objects    i
   int value;    //..snip..  

public:    

   // inspector methods    
   static int getInstanceCount(void) const
   {
return Dummy::instances;
   }    

   int getValue() const { return value; }

   //..snip..    

   Dummy() { instances++; }    
   ~Dummy() { instances--; }
};


Either remove the static before the getInstanceCount or the const after it will make it compile. I think the issue is to do with the const since a static function doesn't have a this pointer (as mentioned above?).

I've asked a guy here at work who is a c++ boffin, I'll let you know what his reply is.

[edit] his reply is this:
the static modifier on a class member function means that no data within the current object is modified by this function.  As the static member function (and the static member variable you're accessing with it) doesn't belong to any object, the const modifier doesn't apply.  Hence, you can't use it so just leave it off.
[/edit]
_________________
Any views, opinions, statements or advice in this message are solely those of the author and do not necessarily represent those of any organisation or individuals.

\\"Don\\\'t make me dance,.... You wouldn\\\'t like me when I dance.\\" - The Hulk
 

Offline TheJackal

  • Jr. Member
  • **
  • Join Date: Oct 2003
  • Posts: 95
    • Show all replies
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #2 on: March 26, 2004, 05:45:40 PM »
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. (Unless of course you passed in a pointer to an instance of a class!)
_________________
Any views, opinions, statements or advice in this message are solely those of the author and do not necessarily represent those of any organisation or individuals.

\\"Don\\\'t make me dance,.... You wouldn\\\'t like me when I dance.\\" - The Hulk
 

Offline TheJackal

  • Jr. Member
  • **
  • Join Date: Oct 2003
  • Posts: 95
    • Show all replies
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #3 on: March 26, 2004, 05:54:18 PM »
my friends reply to my reply is :

Quote

Indeed you are correct, good sir.

Unless the static member function is passed a pointer to an instance of the class (or a pointer to an instance of the class is stored in a static member variable), if this is the case then the static member function can use the pointer to access members of that instance.  It still can't be declared const though.
_________________
Any views, opinions, statements or advice in this message are solely those of the author and do not necessarily represent those of any organisation or individuals.

\\"Don\\\'t make me dance,.... You wouldn\\\'t like me when I dance.\\" - The Hulk
 

Offline TheJackal

  • Jr. Member
  • **
  • Join Date: Oct 2003
  • Posts: 95
    • Show all replies
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #4 on: March 26, 2004, 06:02:50 PM »
Quote

-edit-

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

:roflmao:


Yeah, Even the com port party setting on my machine says it all, "-none-".

 :lol:

_________________
Any views, opinions, statements or advice in this message are solely those of the author and do not necessarily represent those of any organisation or individuals.

\\"Don\\\'t make me dance,.... You wouldn\\\'t like me when I dance.\\" - The Hulk