CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2003
    Posts
    893

    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.

  2. #2
    Join Date
    Sep 2002
    Posts
    1,747
    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

  3. #3
    Join Date
    Apr 2003
    Posts
    893

    Smile 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

  4. #4
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720
    another thing:
    Code:
    static bool MyJNash var_;
    Maybe it should be just:
    Code:
    static bool var_;
    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.

  5. #5
    Join Date
    Apr 2003
    Posts
    893

    Thanks Avdav

    You meant both ways of declaration can also give me the same result ???

    Thanks,
    Nina

  6. #6
    Join Date
    Sep 2002
    Posts
    1,747
    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

  7. #7
    Join Date
    Nov 2003
    Posts
    15
    Thanks a lot,

    Regards,

    Fiona

  8. #8
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    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
  •  





Click Here to Expand Forum to Full Width

Featured