C++ Inheritance confusion

Started by
10 comments, last by frob 10 years, 1 month ago

So, while we're on the topic, what are the direct thoughts on my design there? I.E. using a validator (and potentially throwing) from the constructor (the latter an idea I got from Scott Meyer, iirc) vs. constructing empty then compelling the user to fill in the blanks prior to calling run().

The reason I wanted to have the constructor do the validation and bomb gracefully upon a fail is that I want the interface here to be very difficult to use incorrectly, and there's (at least) two parts to that, which I see: fewer calls need to be made (less internals exposed) and it still prevents you from doing something bonkers (in my example there, you can't do any object-supported operations on your list of things if the place isn't valid).

I think that, were I to allow empty construction then push init responsibility to the user, there's not much of a difference: init() could throw instead of the constructor, and you'd still have an object in an empty/usable state. (That has some appeal.) I'm honestly not clear on what the right answer to this is.

Advertisement

RAII says to initialize your data, and to this I agree.

It is in contrast to other languages (such as C) where a structure can be created but initially contains unspecified garbage, just whatever happened to be in memory at the time. Empty is initialized, random unspecified garbage is uninitialized.

People who come from a more modern language background (C#/Java/Python) are used to members being automatically initialized for you, so when they see RAII they interpret it differently than it was meant. Back in the day when the alternative meant having unspecified values, including invalid values that looked like valid values, RAII meant creating an allocation function that allocated memory and then immediately called something like bzero() or memset() or otherwise immediately assigning valid values if zero is not appropriate, then returned the initialized resource.

In my experience it is fine to allow a convenience non-default constructor if you want, but beware of error conditions.

It is generally best to have a default constructor that only creates and empty object. This way when someone allocates an array or temporaries or other quick-to-do things, they immediately get back the objects without any fuss.

If you want to provide an additional constructor that builds up an object based on additional data you can do so but it adds complexity. Now you have a constructor that is likely to have error conditions (so you may need to throw based on all possible error conditions, in turn forcing you to catch all the exceptions that might be thrown at construction time). Such a constructor takes more time and blocks execution. Such a constructor generally makes some things a little more complicated in exchange for convenience in another area. Yes, for some things there is no additional cost and the difference is that the values are assigned to only once rather than twice; for other objects, however, the cost to the application can be quite large. When such a constructor requires trips to disk, trips to the network, or any other long-running or blocking behavior, I generally question its necessity.

This topic is closed to new replies.

Advertisement