What is the best practice for where to put validation functions when I have multiple classes that need to use them?

Feb 22, 2014 at 2:41am
I'm making various general validation functions to check if the user entered a number, that there are no spaces, etc. and I have many classes that have functions where the user enters some input. It seems arbitrary to just pick a class that needs a lot of validation and put all the general validation functions in there and make functions that need to use validation in other classes friends of that class. I could make a separate class called "validator" to store all the validation functions but I've never heard of anyone doing that before. And I don't know if it's possible to have all the general validation functions in main.cpp/source.cpp and access them from the other classes. I could make a base class called ThingThatNeedsValidation or something and have the other classes inherit from that but again, I've never heard of people doing that before.
Feb 22, 2014 at 2:53am
Does it have to be inside of a class?
What's wrong with leaving them as good ol' standalone functions?
1
2
3
4
5
6
// In your header file:
// ...(stuff here, e.g. #include guards, other function declarations, whatever)

int getIntInput(); // Or whatever validation function you use
// If it's a template function, the definition should go here as well, and you're done
// Otherwise, see below: 
1
2
3
4
5
6
7
8
9
// In your .cpp file:
// ...(stuff here, e.g. #includes, other functions, whatever)
int getIntInput()
{
    int val;
    while (!(cin >> val))
        // Blah blah, you know what to do
}
// ...(more stuff...) 
1
2
3
4
5
6
7
8
9
10
11
12
// In your main.cpp file (or really, any file that needs getIntInput):
// ...(stuff here, e.g. #includes)
#include "whatever_header_file_contains_getIntInput.h"
// ...(stuff here, e.g. more #includes, whatever else you have, etc.)

int main()
{
    // ...(stuff here...)
    int num = getIntInput(); // Compiler only needs to be able to see the declaration
    // It's up to the linker to provide the definition (which should be in a .cpp file)
    // ...(do stuff with 'num')
}

Last edited on Feb 22, 2014 at 2:54am
Feb 22, 2014 at 2:56am
Standalone functions? In main.cpp/source.cpp? You mean make those functions friends of whichever classes that need to use them? I don't really get what you mean by standalone functions... are you saying standalone functions can be in a header file even if they are not in a class? They can be in their own header file?

Most of my program is going to be started by a function called runMenuSystem which is in a class caled MainMenu so I will be doing hardly anything in the main function. I only have 5 lines of code in Main()
Last edited on Feb 22, 2014 at 3:02am
Feb 22, 2014 at 3:18am
Yeah, you can have functions in header files outside of classes.
They shouldn't need to be friend functions, unless you actually reference a private member of a class inside one of them (and I don't mean pass by reference -- I mean actually writing the name of the variable inside the function).

The only thing the compiler needs to see when compiling any code that uses those functions is the declaration. A separate header file would, naturally, be a good place to put those declarations (you would only have to #include the header file containing them -- remember, #include s are basically just copy/pastes).

The definitions can go in a .cpp file (but remember to compile/link that file as well). You can basically stick them almost anywhere you want, but a separate .cpp file seems like a good place.
Feb 22, 2014 at 3:36am
Alright, thanks for letting me know. I had no idea that was possible but that does sound like a good idea.
Topic archived. No new replies allowed.