when (if ever) is = called without you knowing?Function parameters that are passed by value are
copy-constructed. Idem for return values (though that copy-constructor call may be eliminated by the compiler).
also, is it true that the deststructor will always be calledUnless you mess up and write erroneous code (e.g. deleting a derived object via a pointer to base without having made your destructor virtual).
but not always a contructor?Constructors, if any, are always called when you create an object.
sometimes its a copy constructor (like when using containers) or = operator even?Foo a; // Default constructorFoo b(a); // Copy constructor Foo c = b; // Copy constructora = c; // Assignment operator
also, so technically, a = operator doesnt even have to return anythingIt doesn''t have to, but it''s idiomatic to return a
reference to the object (i.e. return *this;)
it does the copying directly in the function?Yes, you do have to write all the copy code yourself.
it only returns *this for chaining? and how does that work exactly?Precisely. a = b = c; is functionally equivalent to a.operator=(b.operator=(c));. By having b.operator=(c); return a reference to b, the code becomes equivalent to b.operator=(c); a.operator=(b). If your assignment operator doesn''t return anything (void), that code is illegal, and you''ll be forced to write b = c; a = b;. Even though you may not find chained assigmnent very useful, people will expect it to work - it works in C, it works for the basic types, why would your class be any different?
does this (&e1 or &e3) say "the address of the pointer", or "the address of the object its pointing to ie the pointer itself"The function is declared as bool operator==(const Enemy& e1, const Enemy& e2) so &e1 and &e2 are the addresses of the
objects. That is the pointers themselves.
Enemy e = new Enemy();Enemy e2 = new Enemy();if(e == e2)
You''re comparing the pointers, not the objects, which means that your operator== is not even called.
so if i wanted to keep the ampersands i would have to de-reference when i sent them?I haven''t read the thread in full - are you looking for a specific object to remove from the container (i.e. knowing its address), in which case you would just do a find based on the value of the pointer, no operator= needed, based on equivalence (i.e. all objects equal to a given one - like two strings with the same text) or are you trying to remove objects from the container based on their properties (i.e. all the ''dead'' objects), in which case find_if with an appropriate predicate would work?
also, i never worked with initialization lists before except when calling a parents constructor from inside of a child class''s constructorPutting member initialization in the initializer list makes it so the members are directly constructed with the proper value, instead of first being default-constructed and then assigned a new value - which is obviously slower. Plus, if a member is either a constant or a reference (or generally, something that is not default constructible or not assignable to), you have no choice but put it in the initializer list.
It is the same difference as between Foo b; b = a; and Foo b(a); (or Foo b = a;.
so you can do this with any function?No. Constructors only.
but really your example does the same thing if you would replace the : with { and then put a semicolon and another bracket
at the end, correct? (ie you just put that code in the body of the constructor)No, not only the syntax isn''t right (you would need to turn it into an assignment), the semantics are different (see above).
another thing that confused me a little was doing
new Data(*rhs.data)...
this is probably a stupid question, but did you purposely not use the = sign or is that just the way you like writing it?It is standard syntax when the constructor for the object you are dynamically creating takes parameters.
lastly, in the = operator, you delete the data then make a new one. why?Read the comments.
since the object already exist''s, shouldnt his constructor allocate the memory for him?The constructor did allocate memory when the object was created, but as I said in the comments, I am handling the general case where you don''t know if the amount of memory that had been allocated originally is sufficient to hold the new data. If Data* were a char*, the original object held enough memory for 6 characters (e.g. it held the string "Hello"), and you want to copy 12 characters in ("Hello world!"), you would need to reallocate memory.
If you are 100% sure that the size of the object never changes, then yes, you can do without the destruction/construction and do a direct assignment (*data = *rhs.data). But then, of course, you''ll have to make sure you don''t need an assignment operator for the Data class.
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan