why overload operator as friend ?

Discussion in 'C++' started by Teddy, Jun 9, 2005.

  1. Teddy

    Teddy Guest

    Hello all

    consider the class Date declaretion below:

    class Date {
    public:
    Date();
    Date(int year, int month, int day);
    Date(const string&);
    int getYear() const;
    int getMonth() const;
    int getDay() const;

    friend bool operator<(const Date&, const Date&);
    friend bool operator>(const Date&, const Date&);
    friend bool operator<=(const Date&, const Date&);
    friend bool operator>=(const Date&, const Date&);
    friend bool operator==(const Date&, const Date&);
    friend bool operator!=(const Date&, const Date&);

    private:
    int years;
    int months;
    int days;
    };

    Can I remove the keyword friend in the six overload operator
    declaretions?
    If I did so, would they still act the same?
    Teddy, Jun 9, 2005
    #1
    1. Advertising

  2. Teddy wrote:
    > Hello all
    >
    > consider the class Date declaretion below:
    >
    > class Date {
    > public:

    [...]
    > int getYear() const;
    > int getMonth() const;
    > int getDay() const;
    >
    > friend bool operator<(const Date&, const Date&);
    > friend bool operator>(const Date&, const Date&);

    [...]
    > private:
    > int years;
    > int months;
    > int days;
    > };
    >
    > Can I remove the keyword friend in the six overload operator
    > declaretions?


    Yes you can, but then there is no need (or not even legal?) to place
    them inside the class.

    > If I did so, would they still act the same?


    Depends on how you have implemented them, if you don't declare them
    as a friend you will not be able to access the private data members
    directly, but only via the public access functions.

    Leonhard
    Leonhard Vogt, Jun 9, 2005
    #2
    1. Advertising

  3. Teddy

    Guest

    Teddy wrote:
    > Hello all
    >
    > consider the class Date declaretion below:
    >
    > class Date {
    > public:
    > Date();
    > Date(int year, int month, int day);
    > Date(const string&);
    > int getYear() const;
    > int getMonth() const;
    > int getDay() const;
    >
    > friend bool operator<(const Date&, const Date&);
    > friend bool operator>(const Date&, const Date&);
    > friend bool operator<=(const Date&, const Date&);
    > friend bool operator>=(const Date&, const Date&);
    > friend bool operator==(const Date&, const Date&);
    > friend bool operator!=(const Date&, const Date&);
    >
    > private:
    > int years;
    > int months;
    > int days;
    > };
    >
    > Can I remove the keyword friend in the six overload operator
    > declaretions?

    No. You'd get a complaint that you cannot have a binary version of the
    operator at this point in the file.
    If you'd alter the entire thing to:
    bool operator<op type>(const Date&) const;
    and alter the function body to work within the class then it would work
    again.

    > If I did so, would they still act the same?

    Yes. If done correctly the user should not be able to see any
    difference in you switching from the binary version to the unary (in
    this case).

    You should be able to go to the unary versions of the operators seeing
    that you have left and right as the same class.
    Generally binary versions are used for things like:
    friend operator<op type>(const <type1>&, const <type2>&);
    friend operator<op type>(const <type2>&, const <type1>&);
    , Jun 9, 2005
    #3
  4. Teddy

    Guest

    I need to think out these replies a bit more.

    operator<op type>(const <class>&) const;
    is still binary (it gets a hidden *this added to the function, which is
    also the reason why
    operator<op type>(const <class>&, const <class>&);
    complains, it expects 2 input vars and gets 3

    Why doesn't this happen if you do
    friend operator<op type>(const <class>&, const <class>&);
    Because you tell your class that a function with this prototype has
    full access
    to the non public parts of your class.
    , Jun 9, 2005
    #4
  5. Teddy

    Howard Guest

    "Teddy" <> wrote in message
    news:...
    > Hello all
    >
    > consider the class Date declaretion below:
    >
    > class Date {
    > public:
    > Date();
    > Date(int year, int month, int day);
    > Date(const string&);
    > int getYear() const;
    > int getMonth() const;
    > int getDay() const;
    >
    > friend bool operator<(const Date&, const Date&);
    > friend bool operator>(const Date&, const Date&);
    > friend bool operator<=(const Date&, const Date&);
    > friend bool operator>=(const Date&, const Date&);
    > friend bool operator==(const Date&, const Date&);
    > friend bool operator!=(const Date&, const Date&);
    >
    > private:
    > int years;
    > int months;
    > int days;
    > };
    >
    > Can I remove the keyword friend in the six overload operator
    > declaretions?
    > If I did so, would they still act the same?
    >


    I see that you have public accessor functions for the private members. So,
    if you remove those friend declarations entirely and just define the
    functions globally (outside the class), then they can use the accessor
    functions (e.g., getYear) to make their comparisons.

    But if you simply remove the friend specifier, then suddenly those become
    member function declarations, and you need to modify the function
    definitions and parameter lists accordingly.

    -Howard
    Howard, Jun 9, 2005
    #5
  6. Teddy

    Swampmonster Guest


    > I need to think out these replies a bit more.
    >
    > operator<op type>(const <class>&) const;
    > is still binary (it gets a hidden *this added to the function, which is
    > also the reason why
    > operator<op type>(const <class>&, const <class>&);
    > complains, it expects 2 input vars and gets 3
    >
    > Why doesn't this happen if you do
    > friend operator<op type>(const <class>&, const <class>&);
    > Because you tell your class that a function with this prototype has
    > full access
    > to the non public parts of your class.
    >


    if you do:

    class foo {
    ...
    return_type operator op_type (const foo&, const foo&);

    you tell the compiler that there is a _member_ function "operator<op
    type>" that returns a "return_type" and accepts an implicit "this"
    pointer and 2 explicit arguments of type "const foo&". therefor the
    compiler will complain because you're declaring a binary operator that
    get's passed 3 arguments (1 implicit "this", 2 explicit).

    if you do:

    class foo {
    ...
    _friend_ return_type operator op_type (const foo&, const foo&);

    you tell the compiler that the _free_ function "return_type operator
    op_type(..." is a friend of class "foo".
    i.e. your allow it access to all protected and private members of "foo".
    and since _free_ functions are not members of any class they have no
    implicit "this" pointer, and hence the 2 arguments are ok (except for
    unary operators of course).

    --

    in the first case (after you remove the 2nd "const foo&" argument) you'd
    have to define the member function like this:

    return_type foo::eek:perator op_type (const foo& right)
    {
    // compare "*this" vs. "right"
    ...

    i.e. qualify the name with "foo::" since the function is a member of
    foo. in the second case you don't have to qualify the name (since it's a
    _free_ function) and only write:

    return_type operator op_type (const foo& left, const foo& right)
    {
    // compare "left" vs. "right"
    ...

    and finally, if you only need "public" access in your _free_ operator
    function, then you can simply delete the whole friend declaration from
    the "foo" class - but you need to delete the whole line, not just the
    "friend" keyword. if you just delete the "friend" keyword you will
    change a "friend declaration" into a "member function declaration" which
    are 2 completely different things. they look similar, but they are not.
    Swampmonster, Jun 10, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gianni Mariani
    Replies:
    5
    Views:
    959
    Jerry Coffin
    Jan 18, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,739
    Smokey Grindel
    Dec 2, 2006
  3. Layton
    Replies:
    2
    Views:
    444
    Layton
    Sep 27, 2006
  4. Mahain
    Replies:
    3
    Views:
    973
    James Kanze
    Dec 20, 2007
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    234
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page