Arrays as Class Member Data Flashcards

1
Q

What is one of the main benefits of having an array as member data of a class?

A

Primitive arrays have no boundary checking and can be quite unsafe to use. Having them as member data means that member functions can be used to prevent out of bounds errors or other undefined behavior.

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

For static member data arrays, what is a useful way to simulate or enforce a current size, even though the actual size is static?

A

A tracking variable which counts up or down depending on the “size” or valid range of indices currently being used by the program. It acts as a sentinel, preventing access beyond it until such time as the “size” changes.

For example, you could set the “size” variable to 0, and even though the array still has the same number of indices and there is data in them, the member functions cannot access any of them, simulating an “empty” list.

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