POD and assignment operator and test operator

H

Hicham Mouline

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,
 
S

Saeed Amrollahi

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
 
J

Juha Nieminen

Hicham said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top