Automatics (Copy Constructors & Assignment Operator) Flashcards
What is an automatic function and what are the four that every class has?
An automatic function is automatically created by the compiler if the user doesn’t create one for the class.
The four automatic functions every class has are:
Constructor
Destructor
Copy Constructor
Assignment Operator =
What is inside the automatic default versions of the constructor and destructor?
Nothing – they are empty.
If a class doesn’t have a default constructor (but does have a parameterized constructor) will a default constructor be automatically created?
No. Not all classes have a default constructor, nor are they required.
What kind of copy do default constructors and copy constructors make?
A shallow copy.
What is a shallow copy?
Copy of all members and data, but any pointers and references are copied verbatim.
They end up pointing to same attached data – that’s not good! They need to be updated to new memory locations or errors are highly likely.
What is a deep copy?
Copy all members and data, but also makes copies of attached data as well (which is external from the physical “object”, such as dynamic memory.)
A deep copy has to be manually written to handle these tasks.
What is a copy constructor?
A constructor invoked automatically when a new copy of an object is created.
When are copies of an object created with the copy constructor?
Copies are made when:
- An object is declared and initialized to another object’s value in the same statement.
// Calls copy constructor
MyObject o2 = o1;
// Different syntax, but identical functionality
MyObject o2(o1);
- An object is passed into (or returned from) a function by VALUE
Will this call the copy constructor?
f1 = f2;
No, it will call the assignment operator.
Explain how to declare a copy constructor and why each component is necessary.
The copy constructor is declared in the format of:
ClassName(const Typename& var);
For example, if the class was MyObject:
MyObject(const MyObject& original);
The ClassName of MyObject is how we know which class this is a constructor of.
The object to be copied MUST BE passed by CONST REFERENCE because it should not be modified – only copied.
I used the variable name “original” to make it very clear when the original’s data is being referenced within the constructor’s definition, e.g. value = value.original;
When is an assignment operator overload invoked? Does it need to be a member function? What is the syntax of the overload’s declaration? Explain each part off that syntax.
The assignment operator overload is invoked any time an assignment statement is made, i.e. something is being assigned to an object.
mo1 = mo2;
Yes, it must be a member function.
// Syntax of assignment overload declaration
Typename& operator= (const Typename& var);
Returning the Typename by reference allows cascading with =. Must be passed by const reference to make a copy without the original being changed.
Example:
MyObject& operator= (const MyObject& original);
What are common considerations of the assignment operator overload for a class?
- It should be treated similarly to a Copy Constructor with regards to deep copies.
- Must return *this by reference, which enables the cascading of the = operator.
- May need previously attached data to be deleted.
- May need protection from self-assignment.
Describe what the keyword ‘this’ is used for.
‘this’ is a pointer to the calling object and can be used like any other pointer (dereferenced with *, arrow operator ->)
It’s used to represent a name for the calling object when such a thing would be necessary, such as returning the object itself.
Write a simple way that you would prevent an object from being assigned to itself.
// Will only run the body of the overload if ‘this’ is not the same address as the original that has been passed in.
if (this != &original)
{
// Further assignment code
}
return *this;
Write a simple way to handle de-allocation in the assignment overload.
// Prevent self-assignment
if (this != &original)
{
// Since this is not a brand new object, // we should delete any information // currently attached: delete [] entryList; // Further assignment code }
return *this;