Hiya, just tryin to get a full grasp of constructors. Below are questions that my study guide asks. I'm wondering if I answered these correctly. If not, please guide me! I have no idea what else you should do if a class has pointer member variables beside deleting it if it's initialized... What else would you need to do if you have pointer member variables? If you think you can add on to my answers, it'd be really helpful. Thanks!
1. What is the purpose of a copy constructor?
a. It is just to copy the contents of an object into a a new object.
2. What are 3 things you should do for a class with pointer member variables?
a. Have a destructor that deletes the pointer.
Never mind. I solved it. It seems that the operator function is only invoked IF the object already exists. In this case, I was declaring and assigning at the same time whereas I should have declared first, and then assigned (2 statements).
What is the main difference between assigning object B to A and declaring object B with A as its parameter? Is there any difference at all? Anything I look up isn't that understandable...
It depends on the behaviour of the class. The first example creates A and B, using the default constructor for myClass in each case. It then uses the assignment operator for myClass to assign new values to B.
The second example creates A using the default constructor for myClass, and B using the copy constructor for myClass.
What the actual results of doing this will be, depends on how you've written the default constructor, the copy constructor, and the assignment operator.
An important thing to remember is that assignment destroys. The line B = A; destroys the contents of B and fills it with new contents (which happen to match the contents of A). Smartly written assignment could reuse as much as it can, but in general, assignment destroys.