Alex Rivera | Logout

Why does the = operator work on structs without having been defined?

Asked 2009-10-15T21:26:06.840
77

Let's look at a simple example:

struct some_struct {
   std::string str;
   int a, b, c;
}

some_struct abc, abc_copy;
abc.str = "some text";
abc.a = 1;
abc.b = 2;
abc.c = 3;

abc_copy = abc;

Then abc_copy is an exact copy of abc.. how is it possible without defining the = operator?

(This took me by surprise when working on some code..)

Edit
Report

1 Answer

4

But it is defined. In the standard. If you supply no operator =, one is supplied to you. And the default operator just copies each of the member variables. And how does it know which way to copy each member? it calls their operator = (which, if not defined, is supplied by default...).

answered 2009-10-15T21:30:19.487

Your Answer