Automatics (Copy Constructors & Assignment Operator) Flashcards

1
Q

What is an automatic function and what are the four that every class has?

A

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 =

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is inside the automatic default versions of the constructor and destructor?

A

Nothing – they are empty.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

If a class doesn’t have a default constructor (but does have a parameterized constructor) will a default constructor be automatically created?

A

No. Not all classes have a default constructor, nor are they required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What kind of copy do default constructors and copy constructors make?

A

A shallow copy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a shallow copy?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a deep copy?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a copy constructor?

A

A constructor invoked automatically when a new copy of an object is created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When are copies of an object created with the copy constructor?

A

Copies are made when:

  1. 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);

  1. An object is passed into (or returned from) a function by VALUE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Will this call the copy constructor?

f1 = f2;

A

No, it will call the assignment operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain how to declare a copy constructor and why each component is necessary.

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are common considerations of the assignment operator overload for a class?

A
  1. It should be treated similarly to a Copy Constructor with regards to deep copies.
  2. Must return *this by reference, which enables the cascading of the = operator.
  3. May need previously attached data to be deleted.
  4. May need protection from self-assignment.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe what the keyword ‘this’ is used for.

A

‘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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write a simple way that you would prevent an object from being assigned to itself.

A

// 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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write a simple way to handle de-allocation in the assignment overload.

A

// 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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly