which are the operators that can be overloaded ??

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

  1. maadhuu

    maadhuu Guest

    hi
    i wasnt to know the answer for the following.

    now ,u can overload all the operators which are basically determined at
    runtime (coz' of whch operators like sizeof())cannot be overloaded.
    now my doubt is , if u have something like

    p->a ....where p(say) is a pointer of a user defined type, then u can
    overload this because p is determined at runtime ???is this right ???
    also if "a" is an object of the some user defined class,

    then is (&a).member valid ?????I thought all "." cannot be overloaded
    ....why ???? (even in such cases)
    maadhuu, Jun 9, 2005
    #1
    1. Advertising

  2. maadhuu wrote:
    > i wasnt to know the answer for the following.
    >
    > now ,u can overload all the operators which are basically determined at
    > runtime (coz' of whch operators like sizeof())cannot be overloaded.
    > now my doubt is , if u have something like
    >
    > p->a ....where p(say) is a pointer of a user defined type, then u can
    > overload this because p is determined at runtime ???is this right ???


    No. If 'p' is a pointer (to anything), it's a built-in type. For it
    operators like * -> + - += -= = == != are predefined and cannot be
    overloaded.

    > also if "a" is an object of the some user defined class,
    >
    > then is (&a).member valid ?????I thought all "." cannot be overloaded
    > ...why ???? (even in such cases)


    If 'a' is of some UDT, then that UDT can have operator & overloaded, and
    returning an object of some other type for which the dot (.) operator is
    used. It's not easy to get the address of that object if you overload the
    operator&, but you may not need to have the address taken.

    V
    Victor Bazarov, Jun 9, 2005
    #2
    1. Advertising

  3. Aaron Gage wrote:
    > Victor Bazarov wrote:
    >
    >>maadhuu wrote:
    >>
    >>
    >>>i wasnt to know the answer for the following.
    >>>
    >>>now ,u can overload all the operators which are basically determined at
    >>>runtime (coz' of whch operators like sizeof())cannot be overloaded.
    >>>now my doubt is , if u have something like
    >>>
    >>>p->a ....where p(say) is a pointer of a user defined type, then u can
    >>>overload this because p is determined at runtime ???is this right ???

    >>
    >>
    >>No. If 'p' is a pointer (to anything), it's a built-in type. For it
    >>operators like * -> + - += -= = == != are predefined and cannot be
    >>overloaded.

    >
    >
    > What are you talking about?!?


    C++. What are YOU talking about?

    > How is a pointer to a user defined class built-in?


    It's a pointer. A pointer is a built-in type.

    OK, let's not argue about this. Go ahead and try to overload operator+
    for your pointer. I DARE YOU!

    >>>also if "a" is an object of the some user defined class,
    >>>
    >>>then is (&a).member valid ?????I thought all "." cannot be overloaded
    >>>...why ???? (even in such cases)

    >
    >
    > if u overload . how will you access class members?
    >
    >
    >>
    >>If 'a' is of some UDT, then that UDT can have operator & overloaded, and
    >>returning an object of some other type for which the dot (.) operator is
    >>used. It's not easy to get the address of that object if you overload the
    >>operator&, but you may not need to have the address taken.
    >>
    >>V

    >
    >
    > I suggest you check out the excellent reference book on C++ by Bruce Eckel:
    >
    > Thinking in C++ Vols 1 and 2:
    >
    > http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
    >
    > There is an entire chapter in Valume One on operator overloading.
    >
    > the only operators you can't overload are . and .*


    Thanks. :)

    I suggest you read it yourself. And other books too. And pay attention
    next time.

    V
    Victor Bazarov, Jun 9, 2005
    #3
  4. maadhuu

    Howard Guest

    "Aaron Gage" <> wrote in message
    news:...
    >>
    >> No. If 'p' is a pointer (to anything), it's a built-in type. For it
    >> operators like * -> + - += -= = == != are predefined and cannot be
    >> overloaded.

    >
    > What are you talking about?!?
    >
    > How is a pointer to a user defined class built-in?
    >


    He's talking about the pointer, not the class. Pointers are built-in types.
    And you can't overload operators which operate on them. You can overload
    the operators that operate on the class it points to, but not ones which
    operate on the pointer itself.

    In other words, this is legal:

    bool operator ==(const MyClass& lhs, const MyClass& rhs)

    as is this:

    bool MyClass::eek:perator ==(const MyClass& rhs)

    but not this:

    bool operator ==(MyClass* lhs, MyClass* rhs)


    -Howard
    Howard, Jun 9, 2005
    #4
  5. maadhuu

    tedu Guest

    Victor Bazarov wrote:

    > It's a pointer. A pointer is a built-in type.
    >
    > OK, let's not argue about this. Go ahead and try to overload operator+
    > for your pointer. I DARE YOU!


    Does this count?

    #include <stdio.h>

    class C {
    public:
    int x;
    };

    typedef C *P;

    P operator+(const P &ptr, const C &val) {
    printf("adding\n");
    return 0;
    };

    int
    main() {
    P a;
    C b;

    a + b;
    }
    tedu, Jun 9, 2005
    #5
  6. tedu wrote:
    > Victor Bazarov wrote:
    >
    >
    >>It's a pointer. A pointer is a built-in type.
    >>
    >>OK, let's not argue about this. Go ahead and try to overload operator+
    >>for your pointer. I DARE YOU!

    >
    >
    > Does this count?
    >
    > #include <stdio.h>
    >
    > class C {
    > public:
    > int x;
    > };
    >
    > typedef C *P;
    >
    > P operator+(const P &ptr, const C &val) {
    > printf("adding\n");
    > return 0;
    > };
    >
    > int
    > main() {
    > P a;
    > C b;
    >
    > a + b;
    > }
    >


    Nice try. No, it doesn't count. You overloaded it for 'C'.
    Do the same for P and an int, for example.

    V
    Victor Bazarov, Jun 9, 2005
    #6
  7. maadhuu

    Howard Guest

    "tedu" <> wrote in message
    news:...
    > Victor Bazarov wrote:
    >
    >> It's a pointer. A pointer is a built-in type.
    >>
    >> OK, let's not argue about this. Go ahead and try to overload operator+
    >> for your pointer. I DARE YOU!

    >
    > Does this count?


    No.

    >
    > #include <stdio.h>
    >
    > class C {
    > public:
    > int x;
    > };
    >
    > typedef C *P;
    >
    > P operator+(const P &ptr, const C &val) {
    > printf("adding\n");
    > return 0;
    > };
    >
    > int
    > main() {
    > P a;
    > C b;
    >
    > a + b;
    > }
    >


    That's not overloading the addition. You're just printing out some text (in
    an overload for the class C, really).

    Try actually adding something to ptr in the operator. Suppose, for
    instance, you put "return ptr + val.x" there. What happens? Oh, it has to
    add something to the pointer. So what does it do? It calls the built-in
    operator for adding an integer to a pointer. The result isn't the value of
    ptr plus val.x, it's the value of ptr plus (val.x times the size of C).

    So, you need an operator that takes an int, not one that takes an object.
    And the compiler won't let you define such an operator.

    -Howard
    Howard, Jun 9, 2005
    #7
  8. maadhuu

    maadhuu Guest

    i did not quite understand the above argument about overloading "+" can you
    please explain a bit more about it ???
    thanx.
    maadhuu, Jun 9, 2005
    #8
  9. maadhuu

    Howard Guest

    "maadhuu" <> wrote in message
    news:...
    >i did not quite understand the above argument about overloading "+" can you
    > please explain a bit more about it ???
    > thanx.
    >


    It might help if you included the specific text that you're asking about,
    don't you think? At least some explanation about what point I made that
    you're not understanding? Preferably, both.

    -Howard
    Howard, Jun 9, 2005
    #9
  10. maadhuu

    maadhuu Guest

    bool operator ==(MyClass* lhs, MyClass* rhs)

    sir, if u could actually tell me why this is not valid....i would actually
    like to know when this sort of thing is valid as in something like

    bool operator == (something1 , something2)
    unless these are static and part of the class or structure as the case
    maybe.(as the left side object calls the function by using the this
    pointer.)

    and when can an operator be overloaded ??

    thanking you,
    maadhuu.
    maadhuu, Jun 10, 2005
    #10
  11. maadhuu

    Aaron Gage Guest

    Victor Bazarov wrote:
    > maadhuu wrote:
    >
    >> i wasnt to know the answer for the following.
    >>
    >> now ,u can overload all the operators which are basically determined at
    >> runtime (coz' of whch operators like sizeof())cannot be overloaded.
    >> now my doubt is , if u have something like
    >>
    >> p->a ....where p(say) is a pointer of a user defined type, then u can
    >> overload this because p is determined at runtime ???is this right ???

    >
    >
    > No. If 'p' is a pointer (to anything), it's a built-in type. For it
    > operators like * -> + - += -= = == != are predefined and cannot be
    > overloaded.


    What are you talking about?!?

    How is a pointer to a user defined class built-in?


    >
    >> also if "a" is an object of the some user defined class,
    >>
    >> then is (&a).member valid ?????I thought all "." cannot be overloaded
    >> ...why ???? (even in such cases)


    if u overload . how will you access class members?

    >
    >
    > If 'a' is of some UDT, then that UDT can have operator & overloaded, and
    > returning an object of some other type for which the dot (.) operator is
    > used. It's not easy to get the address of that object if you overload the
    > operator&, but you may not need to have the address taken.
    >
    > V


    I suggest you check out the excellent reference book on C++ by Bruce Eckel:

    Thinking in C++ Vols 1 and 2:

    http://mindview.net/Books/TICPP/ThinkingInCPP2e.html

    There is an entire chapter in Valume One on operator overloading.

    the only operators you can't overload are . and .*
    Aaron Gage, Jun 10, 2005
    #11
  12. maadhuu wrote:
    > bool operator ==(MyClass* lhs, MyClass* rhs)
    >
    > sir, if u could actually tell me why this is not valid....


    This is not valid because you're not allowed to define your own operators
    if no argument is of a class or enumeration type. Both arguments here are
    pointers. Do you understand the difference between an object being of
    a class type and an object being of a pointer to a class type? If you do
    not, you need to understand it before you proceed.

    > i would actually
    > like to know when this sort of thing is valid as in something like
    > bool operator == (something1 , something2)


    This would be valid if 'something1' _or_ 'something2' have a class type or
    an enumeration type.

    > unless these are static and part of the class or structure as the case
    > maybe.(as the left side object calls the function by using the this
    > pointer.)


    Yes, if it's a member of a class, then one of the arguments is of that
    class type.

    > and when can an operator be overloaded ??


    How many times do I need to repeat the same thing? Get a good book and
    read it, will you?

    V
    Victor Bazarov, Jun 10, 2005
    #12
    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. -Steve-
    Replies:
    2
    Views:
    360
    - Steve -
    Jul 28, 2003
  2. John Harrison
    Replies:
    5
    Views:
    321
    - Steve -
    Jul 29, 2003
  3. Andy Jarrell

    Inheriting overloaded operators

    Andy Jarrell, Nov 13, 2003, in forum: C++
    Replies:
    5
    Views:
    426
    Victor Bazarov
    Nov 13, 2003
  4. Pete Wilson
    Replies:
    1
    Views:
    311
    Mike Wahler
    Apr 3, 2004
  5. NKOBAYE027
    Replies:
    2
    Views:
    346
    NKOBAYE027
    May 8, 2004
Loading...

Share This Page