-
December 13th, 2003, 08:59 PM
#1
very basic question on static reference
PHP Code:
class MyJNash{
static bool MyJNash var_;
static bool equillibrium=true;
public:
static bool MyJNash &(){
if(var_==equillibrium)
return var_;
.........}
I know how to interpret the code but I dont understand that static function with reference used like above...
Could you one more time please give me an explanation on it ???
Thanks so much,
Regards,
FionA
Last edited by hometown; December 13th, 2003 at 09:05 PM.
-
December 13th, 2003, 11:14 PM
#2
I don't understand it either. It looks kind of like a constructor (since it has the same name as the class), but it returns a bool (ctors don't return anything) and is static. The & sign seems out of place. Perhaps you are looking at code from a different language?
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
December 14th, 2003, 05:05 AM
#3
I am so sorry, I forgot the function name...
Originally posted by hometown
PHP Code:
class MyJNash{
static bool MyJNash var_;
static bool equillibrium=true;
.........
public:
static bool MyJNash &JohnFunc(){
if(var_==equillibrium)
return var_;
.........}
Could you help me again ??
Thanks so much for your replies...
Regards,
Fiona
-
December 14th, 2003, 10:30 AM
#4
another thing:
Code:
static bool MyJNash var_;
Maybe it should be just:
or
Code:
static MyJNash var_;
The same thing with the returning type of function, but it wouldn't make the body of the function less confusing.
-
December 14th, 2003, 10:52 AM
#5
Thanks Avdav
You meant both ways of declaration can also give me the same result ???
Thanks,
Nina
-
December 14th, 2003, 12:42 PM
#6
They don't mean the same thing. One gives the name var_ the type bool and the other gives it the type MyJNash.
The static function that is confusing you probably should look like
Code:
static bool &JohnFunc()
without the MyJNash typename in there. Now if you are curious why the & seems "attached" to the function name, instead of the return type, it is because it doesn't matter. Some people write
Code:
ReturnType *someFunction()
where others would write
Code:
ReturnType* someFunction()
Sometimes the reasoning is based on the duality between pointer type and dereferencing. For example, one could then write
Code:
ReturnType myObject = *someFunction();
and someone else could write
Code:
ReturnType *myPointer = someFunction();
depending on whether or not someone wanted ownership of the object lifetime or not and whether / not to copy, etc. And the same types that might put the pointer on the name could put the reference there for symmetry, even though you don't quite have the same duality.
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
December 14th, 2003, 09:40 PM
#7
Thanks a lot,
Regards,
Fiona
-
December 15th, 2003, 02:17 PM
#8
That's not a function with reference, I believe it's an overload of the & (adress-of) operator.
Never before seen it done, and it can cause you some headaches if you actually want the address of the object.
All the buzzt
CornedBee
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|