Best Practices for Having a "Generic" function...

Hello,

I know the title is rather ambiguous so let me explain.

Let's first take the following class prototype:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <ctime>
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>

class Debugger;

class Debugger {
	public:
		Debugger();
		~Debugger();
		
		enum DebugLevel{
			Debug_Level_None,
			Debug_Level_Minimal,
			Debug_Level_Standard,
			Debug_Level_High,
			Debug_Level_Max
		};
		
		std::string GetCurrentDebugLevel();
		DebugLevel GetCurrentDebugLevel();
	
	private:

		DebugLevel CurrentDebugLevel;
}


My question is regarding the GetCurrentDebugLevel function above. Ideally I would like to use just one function to get the current debug level of the class, the std::string form would be used to save the "debugging level" in a settings file and the enumerated version would for evaluation purposes throughout the program.

My question is if implementing function prototypes by return value the best way to go or should I do something else?

Again, I like the ability to just use one function for things like this.

What's the best way?

Thank you.
I wouldn't do it this way. If the only difference is the return type then somewhere, somehow, the rules for function resolution will trip you up and you'll end up calling the wrong function. Consider void GetCurrentDebugLevel(std::string &) const instead. This way there will be no ambiguity.

Personally, I think accessor methods are overused. Is there a compelling reason to make CurrentDebugLevel private with accessors instead of just making it public? With accessors you can't take its address, or increment/decrement it, or get a reference to it, or ....
Thanks; I might try your void function or your second suggestion.

The reason why I always used accessor methods was the control it offered with the access.

In the example above let's consider the following scenario:

Suppose, in using the Debugger class above, once the debug level is set you don't want to change it for the life of the class instance. AKA once the debug level is set to high you don't want to accidentally change the debug level to something else.

With the Get/Set way of doing things this is achievable but I am assuming with the other way you would have to either make CurrentDebugLevel constant or do a check all of the time.

Does that make sense?
Yes, that makes perfect sense.
If the only difference is the return type then somewhere, somehow, the rules for function resolution will trip you up
This isn't really important, as a program with two functions that are only distinguished by their return type will not compile.
Topic archived. No new replies allowed.