Thread: Function overloading....with const as one of the argument

  1. #1
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39

    Cool Function overloading....with const as one of the argument

    My question is that can i overload function in c++ on the basis of declaring one argument as with const (constant).........

    Eg..

    Code:
    int foo(int a)
    {
     bla bla....
    }
    and

    Code:
    int foo(const int a)
    {
    bla bla....
    }
    are these two function same................i did it but got error....

    Function Overloading this link say function overloading can be done with const ....YES is given there
    Last edited by Username changed; 01-11-2014 at 02:29 PM. Reason: i didn't wrote" int" in second function

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > are these two function same................i did it but got error....
    Posting actual code and actual error messages would help.

    All you're going to get is "works for me" posts.
    Code:
    $ cat m1.cpp
    #include <iostream>
    using namespace std;
    
    void foo ( const int *a ) {
      cout << "const " << *a << endl;
    }
    void foo ( int *a ) {
      cout << "var " << *a << endl;
    }
    
    int main ( ) {
      const int a = 1;
      int b = 2;
      foo(&a);
      foo(&b);
    }
    $ g++ m1.cpp
    $ ./a.out 
    const 1
    var 2
    See - works for me!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You've left out "int" from your second function, but it wouldn't work even if you put it in. The reason is that there is no essential difference between those two. It's basically useless to declare an int parameter as const since it's just getting a copy of the value anyway. const is a guarantee to the caller that it's data won't be modified when it's passed by reference or pointer.

    So the way to overload on const is like this:
    Code:
    int foo(int *a) {  // or int &a
        //...
    }
    
    int foo(const int *a) { // or const int &a
        //...
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39
    i used "*" pointer and it worked ...with const

    Code:
    int fun(int b)
    {
        //..........
    }

    Code:
    int fun(const int* b)
    {
       //............
    }
    can u pls tell me IN DETAIL why..now these two function become different......

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your example is getting more stupid. Now you have one that's an int and one that's a (const) POINTER to an int, so obviously they are very different. You ask for "DETAIL" from us, but you yourself are being very sloppy. Re-read the examples above. I suspect that's all you're gettin'.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    OK...... Watch this:
    C++ - 30 - Function Overloading

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Rohit Kumar View Post
    i used "*" pointer and it worked ...with const

    Code:
    int fun(int b)
    {
        //..........
    }

    Code:
    int fun(const int* b)
    {
       //............
    }
    can u pls tell me IN DETAIL why..now these two function become different......
    For some real 'fun', try calling that with zero!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by iMalc View Post
    For some real 'fun', try calling that with zero!
    Or NULL - same thing. To fix that, you should use nullptr if you have a C++11 compiler.

    I'm no expert on the overloading/conversion/promotion riles, but I believe the real reason you can't overload int and const int the way you did is because a number (without postfixes), such as as 5, is pretty much a const int (because it's an int and you can't assign to a number).

    Now, that int can be passed to the int overload (because a copy is made, the compiler won't break constness). But it can also be passed to const int, because again, the same logic applies. Either way, a copy is made, so the compiler cannot decide which function to call.

    In Salem's example, it works because with a const int*, it cannot call the int* function, because that would allow the function to be able to modify the original value, hence breaking the const promise. That leaves only one overload, and that is the one it picks.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by C++11 draft standard

    Parameter declarations that differ only in the presence or absence of const and/or volatile are equivalent. That is, the const and volatile type-specifiers for each parameter type are ignored when determining which function is being declared, defined, or called. [
    Example:
    Code:
    typedef const int cInt;
    int f (int);
    int f (const int); // redeclaration of f(int)
    int f (int) { /* ... */ } // definition of f(int)
    int f (cInt) { /* ... */ } // error: redefinition of f(int)
    —end example
    ]
    Only the const and volatile type-specifiers at the outermost level of the parameter type specification are ignored in this fashion; const and volatile type-specifiers buried within a parameter type specification are significant and can be used to distinguish overloaded function declarations.

    In particular, for any type T, “pointer to T,” “pointer to const T ,” and “pointer to volatile T ” are considered distinct parameter types, as are “reference to T ,” “reference to const T ,” and “reference to volatile T
    I don't really understand it either. I understand that foo(5) would be ambiguous, but how about
    Code:
    void foo(const int i)
    {
      i++; // illegal
    }
    void foo(int i)
    {
      i++; //legal
    }
    
    void caller(void)
    {
      const int i1;
      int i2;
      foo(i2);
    }
    Much like you'd call a function overloaded with other potentially ambiguous types, like different sized integral types. Still, the standard has spoken, and I can't really think of a good reason to want to do this.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, essentially it says that any const and volatile specifiers that are not part of a pointer or reference type are ignored, so

    void foo(int);
    void foo(const int);
    void foo(volatile int);
    void foo(const volatile int);

    are all considered equivalent. But if you add a pointer or reference type, the const or volatile becomes significant (i.e. not ignored), so

    void foo(int*)
    void foo(const int*)
    void foo(int&)
    void foo(const int&)

    are all different signatures.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    And so these are considered equivalent too:

    void foo(int * const)
    void foo(int * )

    (same with & instead of *; or volatile (or const volatile) instead of const)

    Basically, if it doesn't make a difference to the caller, it's equivalent.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You can't overload based on const-ness when it's pass-by value, because that const only says something about the copy, not the original.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Jan 2014
    Posts
    15
    Plus, using const while passing by value should be avoided anyway.

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Plus, using const while passing by value should be avoided anyway.
    O_o

    Nonsense.

    One may avoid it if they desire, but if one desires the "firewall" of the compiler preventing one from accidentally mutating a variable, the approach is fine.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    A gotcha with top level const variables, including parameters, is that if the type is movable, then making it top-level-const prevents it from being moved. This mostly creates an inefficiency error when returning such a variable by value. This is particularly relevant when there are multiple return statements in the function that return different variables, because RVO tends to fail in those cases. It can also create the same problem when you explicitly move the variable, but in that case the error is more obvious.

    So that's a reasonable C++11 reason to avoid them.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2013, 10:13 AM
  2. non-const argument and const pointer
    By nepper271 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2011, 09:59 PM
  3. const reference pass as argument?
    By patiobarbecue in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2009, 12:33 AM
  4. function overloading. const T and const T* parameters
    By Mario F. in forum C++ Programming
    Replies: 7
    Last Post: 06-07-2006, 11:04 AM
  5. const qualifier used on a function argument
    By hzmonte in forum C Programming
    Replies: 27
    Last Post: 04-18-2006, 11:08 PM