Thread: Function Overloading

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Function Overloading

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
       void disp(char *);
       void disp(const char *);
       
       char *a = "Vaibhav";
       const char *b = "Gaurav";
       
       disp(a);
       disp(b);
       getchar();    
    }
    
    void disp(char *a)
    {
         cout<<"char";
    }
    
    void disp(const char *b)
    {
         cout<<endl<<endl<<"const char";
    }
    now a is implicitly const, so shouldnt the both disp() calls go to

    void disp(const char *b)

    however, the run shows, first disp() calling void disp(char *a) and second disp() calling void disp(const char *b)

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It goes to the exact match first. So since a is a char* and there is a function overload for char*, that is the one that is chosen.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Despite the fact that string literals are "constant" in that they usually are placed in read-only memory, their type is still just "char *" not "const char *." A string literal is not "implicitly const," it just happens to be unmodifiable on most platforms.

    When C was first being designed, full memory protection wasn't common and so there was no way to force string literals to be unwritable. So it made no sense to make the type of a string literal be "const char *" because it defied reality. C++ inherited from C and so it inherited this quirk.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Actually, string literals do have the type of const char* in C++. However there is a deprecated feature that allows you to assign them to a regular char*.

    I thought the reason literals were char* in C was that C didn't have const.

  5. #5
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by brewbuck View Post
    Despite the fact that string literals are "constant" in that they usually are placed in read-only memory, their type is still just "char *" not "const char *." A string literal is not "implicitly const," it just happens to be unmodifiable on most platforms.

    When C was first being designed, full memory protection wasn't common and so there was no way to force string literals to be unwritable. So it made no sense to make the type of a string literal be "const char *" because it defied reality. C++ inherited from C and so it inherited this quirk.
    Actually that's not completely true. The standard defines the type of a string literal as “array of n const char”. However the standard explicitly allows the conversion of string literal to "pointer to char".

    it might seem inconsequential but it matters when you're specialising function templates (as opposed to overloading functions)

    for example this code
    Code:
    #include <iostream>
    
    template <typename T>
    int f(T t)
    {
    	return 0;
    }
    
    template <>
    int f<char *>(char *t)
    {
    	return 1;
    }
    
    template <>
    int f<const char *>(const char *t)
    {
    	return 2;
    }
    
    int main()
    {
    	char *p = "test";
    	std::cout << f(1);
    	std::cout << f(p);
    	std::cout << f("test2");
    }
    will print 012.
    Last edited by ChaosEngine; 06-20-2007 at 06:22 PM. Reason: damnit daved beat me to it...stupid example code... bah!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Daved View Post
    I thought the reason literals were char* in C was that C didn't have const.
    K&R C might not have, but I'm pretty sure ANSI C has const. I could be wrong though.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, ANSI C most definitely does have const.
    The following keywords have been added to the language:

    const
    enum
    signed
    void
    volatile
    From http://www.ericgiguere.com/articles/ansi-c-summary.html

    That's the original ANSI C, not amendment 1.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    So why aren't string literals const char* in C89?

    Or are they?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Good question.

    I'm fairly certain that in C89 at least string literals are of type const char*. This thread seems to support this: http://www.thescripts.com/forum/thread441942.html

    But this says otherwise:
    C: The type of a narrow string literal is array of char and the type of a wide string literal is array of wchar_t.

    C++: The type of a narrow string literal is array of const char and the type of a wide string literal is array of const wchar_t. Both types have static storage duration.
    From http://publib.boulder.ibm.com/infoce...rc02stricn.htm
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM