Thread: Operator overloading syntax

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    Operator overloading syntax

    I am looking at a piece of code that overloads an operator, the example I have compiles and works fine, however I don't quite understand it
    Code:
    //definition
    date operator+( const date & lhs, int rhs ); 
    bool operator==(const date &, const date &); //this I don't get
    
    //implementation
    date operator+( int lhs, const date & rhs )
    {
    	return rhs + lhs;
    }
    
    bool operator==(const date & lhs, const date & rhs)
    {
        return lhs.compare(rhs) == 0;
    }
    When I get to the implementation I can follow it again. It's the declaration I am have trouble with.
    Code:
    const date & lhs
    I follow, but
    Code:
     const date &
    I don't. I was expecting a name like lhs to be present??

    Any explaination would be appreciated



  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Forward declarations do not require variable names for the argument:

    Code:
    int someFunc(int, int);
    That will work, but in the implementation you must have a variable name:
    Code:
    int someFunc(int a, int b)
    {
    //...
    
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    Forward declarations do not require variable names for the argument
    I didn't know that, thankyou.

    The example I was looking at did some with variable names and some without.

    Do you think it is better to stick to one way, and if so which one?

  4. #4
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Complete preference. Myself, I like to put in the names.
    Do not make direct eye contact with me.

  5. #5
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    I think putting the names are useful in complex functions (look at the Windows API if you don't see what I mean -_-) where types aren't sufficient to tell what's the parameter role.
    Well, if you don't know, just stick with the full qualification stuff. But, for an operator, the arguments are predefined by the langage (except for the () op.) so there's generally no need for names.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Putting in the names is also useful for helper tools like VC++'s IntellliSense, which presents you with the declaration of a function when you open the call parenthesis.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    in the implementation you must have a variable name
    Not true. If you do not use the variable in the function, it is not necessary to name it. For example:
    Code:
    int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
    {
    return 0;
    }
    This is perfectly valid.

    Personally, I don't specify variable names in funtion prototypes, because if for some reason I later decide to change the variable name, I must change it in two spots. Not a big effort I suppose, but to me it just seems the right way to do things. If you want to know each parameter's function by looking at the prototype, tack a comment on the end.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by bennyandthejets
    Not true. If you do not use the variable in the function, it is not necessary to name it. For example:
    Code:
    int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
    {
    return 0;
    }
    This is perfectly valid.

    Personally, I don't specify variable names in funtion prototypes, because if for some reason I later decide to change the variable name, I must change it in two spots. Not a big effort I suppose, but to me it just seems the right way to do things. If you want to know each parameter's function by looking at the prototype, tack a comment on the end.
    Not true, it's perfectly legal to specify DIFFERENT names. Only the one in the definition matters; the name in the declaration is unused. And sometimes I DO make them different -- I want the names to be maximally descriptive in the declarations, but sometimes I don't use the same names (for various reasons) in the definition.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM