POD and assignment operator and test operator

Discussion in 'C++' started by Hicham Mouline, Sep 1, 2009.

  1. Hello,

    struct S {
    double d1;
    double d2;
    };

    S s1,s2;

    what was the reason that

    s1=s2; // assignment operator (default defined for PODs, right?)

    was valid, but not

    if (s1 == s2) // test for equality

    Regards,
    Hicham Mouline, Sep 1, 2009
    #1
    1. Advertising

  2. On Sep 1, 8:10 pm, "Hicham Mouline" <> wrote:
    > Hello,
    >
    > struct S {
    >   double d1;
    >  double d2;
    >
    > };
    >
    > S s1,s2;
    >
    > what was the reason that
    >
    > s1=s2;  // assignment operator (default defined for PODs, right?)
    >
    > was valid, but not
    >
    > if (s1 == s2)  // test for equality
    >
    > Regards,


    Hi Hicham

    Because the first one is copy assignment operator, but the second one
    is equality operator.
    The first one is declared and defined by compiler implicitly with
    default behavior,
    but in equality operator case , you have to declare and define its
    behavior explicitly.

    Regards,
    -- Saeed Amrollahi
    Saeed Amrollahi, Sep 1, 2009
    #2
    1. Advertising

  3. Hicham Mouline wrote:
    > what was the reason that
    >
    > s1=s2; // assignment operator (default defined for PODs, right?)
    >
    > was valid, but not
    >
    > if (s1 == s2) // test for equality


    If the compiler was to automatically generate code for the equality
    comparison (which would apply the equality comparison to every member),
    wouldn't the next question be why the compiler is not doing the same for
    operator<() and the other comparison operators?

    But in that case, how would the compiler implement those operators?
    Juha Nieminen, Sep 1, 2009
    #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. Ajax Chelsea

    Is array of POD still a POD type?

    Ajax Chelsea, Dec 1, 2003, in forum: C++
    Replies:
    1
    Views:
    336
    Ron Natalie
    Dec 1, 2003
  2. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    480
    Barry Schwarz
    Feb 8, 2005
  3. Ivan A. Kosarev

    Operator new and POD-struct

    Ivan A. Kosarev, Dec 1, 2005, in forum: C++
    Replies:
    6
    Views:
    320
  4. Chris
    Replies:
    34
    Views:
    1,491
  5. Himanshu Garg
    Replies:
    1
    Views:
    152
    Himanshu Garg
    Sep 21, 2003
Loading...

Share This Page