Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Function Overloading

Function Overloading

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amrit Agr
    wrote on last edited by
    #1

    Dear Developers, Todays I found a surprsing fact on the scenario of functiona overlaoding in C++. The code segment is here #include using namespace std; void display( char* ); void display( const char* ); void main() { char *ch1 = "Hello"; const char *ch2 = "Bye"; display(ch1); display(ch2); } void display( char* p ) { cout << p; } void display( const char* p ) { cout << p; } As I know, In function overloading, the arguments must be differ in either in type or count. But its working. and one more surpring fact is that if I am remving pointers on that program. It says.. that function already have a body. How is it possible???? Please guide me. Thnaks. Amrit Agrawal Software Developer, Mumbai

    J S 2 Replies Last reply
    0
    • A Amrit Agr

      Dear Developers, Todays I found a surprsing fact on the scenario of functiona overlaoding in C++. The code segment is here #include using namespace std; void display( char* ); void display( const char* ); void main() { char *ch1 = "Hello"; const char *ch2 = "Bye"; display(ch1); display(ch2); } void display( char* p ) { cout << p; } void display( const char* p ) { cout << p; } As I know, In function overloading, the arguments must be differ in either in type or count. But its working. and one more surpring fact is that if I am remving pointers on that program. It says.. that function already have a body. How is it possible???? Please guide me. Thnaks. Amrit Agrawal Software Developer, Mumbai

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      Use code tags when you post code. What do you mean by "removing pointers"?

      Amrit Agr wrote:

      the arguments must be differ in either in type or count. But its working.

      The arguments to the methods have different types. And do you have warnings turned on to the highest level in your C++ compiler?

      1 Reply Last reply
      0
      • A Amrit Agr

        Dear Developers, Todays I found a surprsing fact on the scenario of functiona overlaoding in C++. The code segment is here #include using namespace std; void display( char* ); void display( const char* ); void main() { char *ch1 = "Hello"; const char *ch2 = "Bye"; display(ch1); display(ch2); } void display( char* p ) { cout << p; } void display( const char* p ) { cout << p; } As I know, In function overloading, the arguments must be differ in either in type or count. But its working. and one more surpring fact is that if I am remving pointers on that program. It says.. that function already have a body. How is it possible???? Please guide me. Thnaks. Amrit Agrawal Software Developer, Mumbai

        S Offline
        S Offline
        smags13
        wrote on last edited by
        #3

        Hi, Amrit, It's working because it's legal to overload functions based on whether the argument refers to a const or non-const type. So,

        void display(const char*) //pointer pointing to a const type

        is overloading

        void display(char*) // plain pointer

        And you're correct. The following couldn't be overloaded. It won't compile because of const conversion used by compiler. It can't figure out which one is the best match.

        void display(char);
        void display(const char);
        void display(char const);

        (Technically, the above are redeclaration. It's fine to just define the third one by adding function body. But it won't compile if all of these three functions have function bodies or definitions.) By the way, to be clearer, the following won't compiler neither because of the same reason.

        void display(char*){...} //plain pointer pointing to a regular type
        void display(char* const){...} //const pointer pointing to a regular type

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups