[Dev-C++] Re: Design decision in C++
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Kevin B. <kev...@ma...> - 2004-03-10 17:24:22
|
The main advantage to the static idea, of course, is that there will be no "this" pointer passed to all your calls, which will make them slightly more efficient. The disadvantages are that the class will be very difficult to generalize and reuse, and that the syntax will seem "weird" and probably a bit difficult to understand for anybody else who ever needs to maintain this class. Personally, I would disregard the "efficiency advantage", since it will likely be utterly insignificant. You should consider an "optimization" like this only when you have clear evidence from performance profiling of a working program that it will make some difference. The key is objective evidence; developers are notoriously bad at guessing where their performance bottlenecks lie. Resist the temptation to over-optimize before you know what the bottlenecks are, and especially before you even have a working design. The rule is, first make it correct, then make it fast. If you're going to make all the class members static, you might as well just forget the class and define plain old variables and functions in a namespace. That would be syntactically almost identical, and conceptually cleaner. This all-static idea is *not* the same as a singleton pattern. A singleton class in general has no more than one instance at a time. With an all-static class, by contrast, there's no point having any instance at all. There's nothing for the "this" pointer to point to, so why bother? Ergo, if you're going to go this route, you might as well just use a namespace instead and avoid confusing future maintainers (and maybe even yourself) with a pointless class declaration. --- Kevin |