so i was trying to find out how to do unbuffered input in linux and came across this class: http://www.cplusplus.com/forum/general/29137/#msg157988 . i didnt like how i had to create a new instance of it each time i wanted to use it, so i made the functions static and renamed the class to Buffer. i could then call it like this: Buffer::On(); Buffer::Off();. My question is, when doing something like that where the class consists of two functions that can exist indepently of another, which is better: a class like what i did or wrapping it in a namespace?
I'd say use a class if you have variables or other methods you want the functions to share without allowing just anyone to mess with them (use access specifiers instead of having them free in the namespace scope). Otherwise I'd go with a namespace.
That should've never been a class. There is no shared information. Only a struct that is reused in each member function (but that should've been a local variable in each function.)
Zhuge wrote:
I'd say use a class if you have variables or other methods you want the functions to share without allowing just anyone to mess with them (use access specifiers instead of having them free in the namespace scope). Otherwise I'd go with a namespace.
There's no need for any variables you want to keep "safe" from even being visible in the namespace solution. Simply don't put them in the header, and give them internal linkage in the .cpp file by placing them in an anonymous namespace. Of course, you could do the same for a static class, but why not make your intentions clear?