using pointer in a friend operator +

Discussion in 'C++' started by =?utf-8?B?5rW36aOO?=, Aug 8, 2006.

  1. Normally, when i define a friend operator + the argument used by the
    operator function is references. but i try to use pointer and look like
    this:
    class A;
    friend A operator+( A *a1, A *a2);

    I cannot pass through the compiler. get error c2803.
    i look up the book<The C++ programming language> ,and i get the reason,
    Pointers cannot be used because it is not possible to redefine the
    meaning of
    an operator applied to a pointer. i don't understand redefine, can you
    tell me ,thanks.
     
    =?utf-8?B?5rW36aOO?=, Aug 8, 2006
    #1
    1. Advertising

  2. =?utf-8?B?5rW36aOO?=

    Ian Collins Guest

    海风 wrote:
    > Normally, when i define a friend operator + the argument used by the
    > operator function is references. but i try to use pointer and look like
    > this:
    > class A;
    > friend A operator+( A *a1, A *a2);
    >
    > I cannot pass through the compiler. get error c2803.


    Which is?

    > i look up the book<The C++ programming language> ,and i get the reason,
    > Pointers cannot be used because it is not possible to redefine the
    > meaning of
    > an operator applied to a pointer. i don't understand redefine, can you
    > tell me ,thanks.
    >

    What possible reason could there be to redefine pointer addition? This
    is what you are attempting to declare.

    --
    Ian Collins.
     
    Ian Collins, Aug 8, 2006
    #2
    1. Advertising

  3. * 海风:
    > Normally, when i define a friend operator + the argument used by the
    > operator function is references. but i try to use pointer and look like
    > this:
    > class A;
    > friend A operator+( A *a1, A *a2);


    The above is not your actual code; please use the copy and paste method
    to ensure the code you post is accurate, the same as the code your
    compiler complains about (otherwise there's not much point).

    > I cannot pass through the compiler. get error c2803.
    > i look up the book<The C++ programming language> ,and i get the reason,
    > Pointers cannot be used because it is not possible to redefine the
    > meaning of
    > an operator applied to a pointer. i don't understand redefine, can you
    > tell me ,thanks.


    You cannot redefine what 'a + b' means when p1 and p2 are both of
    built-in type or pointer.

    For example, you can't define 'int operator+( int a, int b )'.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Aug 8, 2006
    #3
    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. Avinash

    Friend and Operator []

    Avinash, Jun 24, 2003, in forum: C++
    Replies:
    5
    Views:
    1,139
    John Carson
    Jun 24, 2003
  2. Nitin Bhardwaj
    Replies:
    8
    Views:
    946
    Andrey Tarasevich
    Jul 14, 2003
  3. Manish_Ganvir
    Replies:
    13
    Views:
    1,603
    Keith Thompson
    Feb 14, 2005
  4. Mahain
    Replies:
    3
    Views:
    1,075
    James Kanze
    Dec 20, 2007
  5. Peter
    Replies:
    2
    Views:
    288
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page