C++ Style Question

When creating a class, in order to do validity checking for class member values, there are two options:

1. Inside your constructor, you could do the validity checking.

2. Or, inside your constructor you could call a set function which does the validity checking.

What is the advantage of making a call to a set function to do validity checking versus doing the validity checking in the constructor function itself. Just seems like unnecessary overhead to me.

Any opinions?

good question.

well, making a seperate validity-checking function and importing it makes sense if you are going to be changing the validity code a lot and reusing it elsewhere.

that way you can just change the validity checker in one place, instead of in 5....

Yeah, Rob is right. It's for the same reason that functions exist, in general.

Unless you only had one class, then I guess it wouldn't really matter, except that it would be easier to reuse the code later on, if need be.

Good answer, thanks Rob.

I was working with the concept of one class. I could see how it would be useful for several.

First of all, what life's purpose does the class have? What are you trying to do?

From a generic standpoint, if my class requires data, I have an overloaded constructor taking in required values. That gives the option to the developer to load everything on instanciation. If not, then you probbly need to validate the data with a private function depending on the method call. Just my $.02.