Thanks for everyone's replies, you guys are awesome!
@Furry Guy
Yes, Sam's 8 Ed.. I zipped through the first 8 chapt & then came to a crawl on Classes & just started operators yesterday. I knew of variables, for loops, do....etc from my dabbling in VB and so that part was easy enough. The book is a great introduction, but every time I turn around there are new tidbits beyond the book. I wonder if I should have just started on a new full-scale book, maybe Bjarne Stroustrup's "The C++ Programming Language, 4th Ed"..any recommendations? I would be more interested in a book that spells things out, so I can clearly understand the topics & not one that makes assumptions. I am 7 weeks into my start with C++ & I will beat it into me!
@Helios
From the book the scope resolution operator, dot, & () when referring to classes & their members frustrated the hell out of me. Until Helios made that mini program showing the differences, that was very helpful & thanks Helios! And hearing you say the next quote...reiterates that this is just known & it is just the way it is...as opposed to experienced programmers would never do that & there is a better way.
1) The change is fine, as long as you understand that a polymorphic clone() member function is supposed to return a pointer to the base class that points to an instance of the derived class whose value is a copy of the this object. |
@Albatross
1. return new Tuna(*this); and return new Tuna; are semantically different. The former invokes the copy constructor, the latter invokes the default constructor. |
Ah yes, that is exactly the insight I needed as I thought they were exactly the same...makes sense when I look at it again now.
return new Tuna; //Creates a new Tuna with no regard to "this" Tuna, but they just happen to be the same. But if we added a single int num; variable here with a copy constructor & assignment operator, then it is a big mistake.
return new Tuna(*this); //This has the potential to copy entirely this Tuna. I have to be honest that I am disturbed by (*this) usage as I would have tried this->Tuna or (*this).Tuna before even thinking the above syntax was possible.
______________
The book mentions smart pointers & I saw the first line in the operator chap yesterday coincidentally. Good to know they exist, will cover them in future chap.
Book also gave example on a char* buffer pointer passed to a global function that needs to ensure a deep copy via a copy constructor. So when you copy a pointer, it makes a new pointer address location, stores the address of the pointer being copied (a copy of it), but does not store the value of what is pointed to...for instance the int. And this applies to all attributes? Did I get that right? I also think the author made the code simpler for understanding, which I am glad he did...for an intro.