parameterless constructor question

A sample problem I've become stuck on....


Sally The Programmer is working to improve a class called FlashDrive. She decides to make a parameterless constructor so that driver code can conveniently say:

FlashDrive f;

and have everything still compile and work properly. In her implementation of the parameterless constructor, she writes:

FlashDrive::FlashDrive() {
// empty...
}

In our book's practice problems we are asked what is wrong with her approach and what would you recommend she do to avoid potential problems?

I can't seem to determine the fault in this
(Assuming FlashDrive has some member variables) None of f's member variables will be initialized so will likely contain garbage values. This leaves f in an unusable state. Initializers should always do what the name implies (even ones that take no argument); an initializer should initialize.
A flash drive might have a substantial amount of initialization that must take place. Whatever the initialization entails, you don't want to repeat yourself by duplicating it in every new constructor. You want the entire initialization process to be described in only one place.

Think about how you can have the initialization be described in only one method, while still ensuring that the initialization happens wether or not you call the parameterized, or default constructor.

Last edited on
Topic archived. No new replies allowed.