|
Register or have you forgotten your password?
|
|
|
| Amiga OS -- Development This particular forum deals with issues regarding development for all versions of AmigaOS. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | ||||||||
|
Sockologist
![]()
|
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 ![]() -/edit- Code:
[size=x-small]
class Dummy {
private:
static int instances; // current number of Dummy objects
int value;
//..snip..
public:
// inspector methods
static int getInstanceCount() const { return instances; }
int getValue() const { return value; }
//..snip..
Dummy() { instances++; }
~Dummy() { instances--; }
};
[/size]
)), 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?
__________________
OCA This isn't SCSI... This is SATA!!! I have CDO. It's like OCD except all the letters are in ascending order. The way they should be. Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64 A1XE 800MHz / 512MB / Radeon 9200 / OS4.1 A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4 A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9 A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB |
||||||||
|
|
|
|
|
#2 | ||||||||
|
Too much caffeine
![]()
Join Date: Oct 2003
Posts: 95
|
have you tried
Code:
[size=x-small]
class Dummy {
private:
static int instances; // current number of Dummy objects
int value;
//..snip..
public:
// inspector methods
int getInstanceCount() const { return Dummy::instances; }
int getValue() const { return value; }
//..snip..
Dummy() { Dummy::instances++; }
~Dummy() { Dummy::instances--; }
};
[/size]
__________________
_________________ 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 |
||||||||
|
|
|
|
|
#3 | ||||||||
|
Too much caffeine
![]()
Join Date: Apr 2003
Posts: 148
|
Hi Karlos
A specialist in finding GNU bugs, huh? ;-) I belive you're right and that should be accepted. However I think I understand the reason why this behaves like so. In C++ to C metacode conversion for methods you could imagine an additional argument: void C::Method( ... ); -> void C_Metchod( C *this, ... ); And for inspectors (I was at important talk recently and someone was checking my knowledge and asked me about this and I didn't know the name...) this changes into: coid C::Inspect( ... ) const; -> void C_Inspect( const C *this, ... ); The reason for this behaviour can be that your method is inlined and then compiler discovers that 'this' is not used, so it removes it completely. If it does not exist it cannot be const. However, like you, I think this is not correct reaction. My last conclusion is that your method is actually static function and its best declaration would be: static int getInstanceCount() { return instances; } as 'this' is really not needed here. And I've met this already somewhere that static methods cannot be declared as inspectors - I belive its because of the above reason. On the other hand this mayby should be accepted also? Cheers |
||||||||
|
|
|
|
|
#4 | ||||||||
|
Sockologist
![]()
|
@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.
__________________
OCA This isn't SCSI... This is SATA!!! I have CDO. It's like OCD except all the letters are in ascending order. The way they should be. Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64 A1XE 800MHz / 512MB / Radeon 9200 / OS4.1 A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4 A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9 A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB |
||||||||
|
|
|
|
|
#5 | ||||||||
|
Sockologist
![]()
|
@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
__________________
OCA This isn't SCSI... This is SATA!!! I have CDO. It's like OCD except all the letters are in ascending order. The way they should be. Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64 A1XE 800MHz / 512MB / Radeon 9200 / OS4.1 A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4 A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9 A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB |
||||||||
|
|
|
|
|
#6 | ||||||||
|
Sockologist
![]()
|
@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...
__________________
OCA This isn't SCSI... This is SATA!!! I have CDO. It's like OCD except all the letters are in ascending order. The way they should be. Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64 A1XE 800MHz / 512MB / Radeon 9200 / OS4.1 A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4 A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9 A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB |
||||||||
|
|
|
|
|
#7 | ||||||||
|
Too much caffeine
![]()
Join Date: Apr 2003
Posts: 148
|
Ok Karlos
I understand and can share your doubts - inspector that does not change static members. However I belive that the implementation is as I described - const or not const 'this' pointer. And static members are sort of globals, only their names are mixed with the name of the class... And how could you declare a function that does not modify global values? The place that I met, rejecting static inspector is HP-UX aCC compiler, quite new one. Cheers |
||||||||
|
|
|
|
|
#8 | ||||||||
|
Sockologist
![]()
|
Bleh,
It's discrepencies like this give C++ a bad name from java coders ![]() 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 ;-)
__________________
OCA This isn't SCSI... This is SATA!!! I have CDO. It's like OCD except all the letters are in ascending order. The way they should be. Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64 A1XE 800MHz / 512MB / Radeon 9200 / OS4.1 A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4 A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9 A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB |
||||||||
|
|
|
|
|
#9 | ||||||||
|
Too much caffeine
![]()
Join Date: Oct 2003
Posts: 95
|
This also happens on msdev c++ compiler:
Code:
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--; }
};
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 |
||||||||
|
|
|
|
|
#10 | ||||||||
|
Sockologist
![]()
|
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 ;-)
__________________
OCA This isn't SCSI... This is SATA!!! I have CDO. It's like OCD except all the letters are in ascending order. The way they should be. Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64 A1XE 800MHz / 512MB / Radeon 9200 / OS4.1 A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4 A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9 A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB |
||||||||
|
|
|
|
|
#11 | ||||||||
|
Too much caffeine
![]()
Join Date: Oct 2003
Posts: 95
|
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 |
||||||||
|
|
|
|
|
#12 | |||||||||
|
Sockologist
![]()
|
Quote:
__________________
OCA This isn't SCSI... This is SATA!!! I have CDO. It's like OCD except all the letters are in ascending order. The way they should be. Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64 A1XE 800MHz / 512MB / Radeon 9200 / OS4.1 A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4 A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9 A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB |
|||||||||
|
|
|
|
|
#13 | |||||||||
|
Too much caffeine
![]()
Join Date: Oct 2003
Posts: 95
|
my friends reply to my reply is :
Quote:
__________________
_________________ 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 |
|||||||||
|
|
|
|
|
#14 | ||||||||
|
Sockologist
![]()
|
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 ![]() -edit- Programmers :-D Man, do we know how to party on friday night, or what?
__________________
OCA This isn't SCSI... This is SATA!!! I have CDO. It's like OCD except all the letters are in ascending order. The way they should be. Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64 A1XE 800MHz / 512MB / Radeon 9200 / OS4.1 A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4 A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9 A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB |
||||||||
|
|
|
|
|
#15 | |||||||||
|
Too much caffeine
![]()
Join Date: Oct 2003
Posts: 95
|
Quote:
__________________
_________________ 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 |
|||||||||
|
|
|
![]() |
| Bookmarks |
| Tags |
| ansi , oddness , c++ , mode , gnu |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| ANSI C++ forbids declaration `res' with no type | EDanaII | Amiga OS -- Development | 18 | 05-03-2009 09:39 AM |
| ansi capable telnet application for OS3.x? | AmiKit | Amiga Software Issues and Discussion | 5 | 04-09-2009 12:37 PM |
| How do I check size/date of a file in ANSI C? | ChaosLord | Amiga OS -- Development | 10 | 02-16-2006 04:17 AM |
| Karlos' weekly GNU/ANSI C++ on amiga bash :-) | Karlos | Amiga OS -- Development | 5 | 04-08-2004 04:43 PM |
| Longs, ints and printf (ansi-C) | elendil | Amiga OS -- Development | 7 | 03-15-2004 08:56 AM |