CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2012
    Posts
    2

    Why do they use '_' character ?

    Code:
    int CmpFunc(const void* _a, const void* _b)
    {
      // you've got to explicitly cast to the correct type
      const float* a = (const float*) _a;
      const float* b = (const float*) _b;
    
      if(*a > *b) return 1;              // first item is bigger than the second one -> return 1
      else if(*a == *b) return  0;         // equality -> return 0
      else         return -1;         // second item is bigger than the first one -> return -1
    }
    They use a '_' character as the first character of variable _a, I know this is a way to declare a variable. My question is: what is the advantage of this declaration? Please tell me, provide links or suggest books if any. Thanks !

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Why do they use '_' character ?

    So that the variables a and b can be used in the body of the function, corresponding to _a and _b, but with the desired type. There is no advantage since the author could just have easily used x and y, lhs and rhs, a_ and b_, etc.

    In fact, the is a potential disadvantage: in C++, names that begin with an underscore are reserved to the implementation for use in the global namespace, and names that begin with an underscore followed by an uppercase letter (or that contain consecutive underscores) are reserved to the implementation for any use. So, _a and _b are okay here, but not okay everywhere.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2012
    Posts
    2

    Re: Why do they use '_' character ?

    Quote Originally Posted by laserlight View Post
    So, _a and _b are okay here, but not okay everywhere.
    I didn't catch you. Can you explain with more information ?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Why do they use '_' character ?

    Quote Originally Posted by superkibo
    I didn't catch you. Can you explain with more information ?
    Basically, under some circumstances, but not in this case, the names _a and _b may be reserved to the compiler or standard library implementation.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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