Jump to content
  • Advertisement
Sign in to follow this  
Noods

Function declared twice?

This topic is 3694 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 }

Share this post


Link to post
Share on other sites
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 ]code here[ /code ] (but without spaces in the brackets).

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!