Function declared twice?

Started by
2 comments, last by Noods 15 years, 11 months ago
I am going to show my lack of C++ knowledge here. Why is XmlTest declared twice? And as a side question, what is the tag for code in a post? /* Test program for TinyXML. */ #ifdef TIXML_USE_STL #include <iostream> #include <sstream> using namespace std; #else #include <stdio.h> #endif #if defined( WIN32 ) && defined( TUNE ) #include <crtdbg.h> _CrtMemState startMemState; _CrtMemState endMemState; #endif #include "tinyxml.h" static int gPass = 0; static int gFail = 0; bool XmlTest (const char* testString, const char* expected, const char* found, bool noEcho = false) { bool pass = !strcmp( expected, found ); if ( pass ) printf ("[pass]"); else printf ("[fail]"); if ( noEcho ) printf (" %s\n", testString); else printf (" %s [%s][%s]\n", testString, expected, found); if ( pass ) ++gPass; else ++gFail; return pass; } bool XmlTest( const char* testString, int expected, int found, bool noEcho = false ) { bool pass = ( expected == found ); if ( pass ) printf ("[pass]"); else printf ("[fail]"); if ( noEcho ) printf (" %s\n", testString); else printf (" %s [%d][%d]\n", testString, expected, found); if ( pass ) ++gPass; else ++gFail; return pass; } int main() { //code }
Advertisement
It's called "function overloading". Doing a google on "C++ Function Overloading" will give you some great info. Essentially, you can have multiple functions with the same name as long as each function takes different parameters. In the above code, notice how the two functions have different parameters. A programmer may want to overload a function for multiple reasons. One example is overloading a function named print. If you have two functions called print, one takes a string and one takes an int, you can then print either a string or an int by calling print(variable) and the compiler will chose which function to call based on what type variable is.

Also, the tags can be found here. To do a source tag, write [ source ]source code here[ /source ] (but without spaces in the brackets). To do a code tag, write code here (but without spaces in the brackets).
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
C++ allows multiple functions to share the same name but to take different parameter sets. The compiler can figure out which function to call based on which types are used in the calling code.

It is interesting to note that were the functions written with C++ in mind (using std::string and std::cout) then a single template function could be written to cater for both (and even more) parameter sets.

Code can use be placed between source tags: [source][/source]
Thanks!

This topic is closed to new replies.

Advertisement