Similar to that, but not exactly like that. Otherwise you'd end up in preprocessor hell. How about defining a portable interface, then just changing the code behind the interface when you port the code:If you want to use it then put it in preprocessor directives like this.
You call clear_screen() however many umpteen times you need to, but portability isn't affected because to go to Linux you only need to change one thing in one place, "CLS" to "clear" in the definition of clear_screen(). A lot of people seem to think portable code means you can compile everywhere without change. That's the ideal, sure. But almost any non-trivial program has to use non-portable stuff, so portability really means how easy it is to get code to compile somewhere else. A portable program fixes the deck so that there's only one point of change for any given non-portable feature.Code:void clear_screen() { system("CLS"); }
You can get the ideal by hacking each port into the program with the preprocessor, but that gets ugly and unwieldy very fast. I don't consider it ideal after two branches, even if you do it intelligently to avoid littering the code with preprocessor directives. If you can reach the ideal in a clean way, great! If not--like with clearing the screen--fix the deck for portability and go have a pint.