Let’s say you have few functions:
func_1(arg1, arg2, arg3) ,
func_2 (arg1, arg2) and
func_3 (arg1, arg2, arg3).
All 3 performing similar steps:
func (arg1, arg2, arg3)
{
Read line from the file
validate_func()
Store results into arg3
}
So my question is – what would best way to approach this in order to combine these 3 functions into 1?
My thoughts to approach this problem to use function with variable number of arguments (va_start, va_end) . What i’m not sure how to approach is to call appropriate validation function – will it be possible to use in this case function pointer as one of the arguments?Not sure at this point
Any suggestions, links, examples are welcome
Thanks
old_jefrey
0
Newbie Poster
Recommended Answers
Jump to Post>>what would best way to approach this in order to combine these 3 functions into 1?
The best way to do this is to not do it. Functions are there to help create more readability for the coder and the reader. The more the better. By trying to combine
…
Jump to PostThen that means you need to abstract the problem. Create 1 common function, that
does a common functionality that each of the other function does, and inside the other function, call that common function.
Jump to PostYou need to abstract when possible. So in the example you gave, create a prase
function and call it on each function.template<typename ReturnType, typename InputType> ReturnType void prase(const InputType& in){ /* do praising here */ /* and return whatever you need to return */ }
All 9 Replies
mrnutty
761
Senior Poster
old_jefrey
0
Newbie Poster
mrnutty
761
Senior Poster
old_jefrey
0
Newbie Poster
mrnutty
761
Senior Poster
mike_2000_17
2,669
21st Century Viking
Team Colleague
Featured Poster
old_jefrey
0
Newbie Poster
old_jefrey
0
Newbie Poster
mike_2000_17
2,669
21st Century Viking
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.