Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7523751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:53:37+00:00 2026-05-30T02:53:37+00:00

I just wanted to know What are the factors decisive in making a function

  • 0

I just wanted to know What are the factors decisive in making a function call during
overloading. As I know its the signature of parameters, number of parameters passed
Which plays an important role. But is there the const part also Which plays an important
part in overloading or not. Overloading of 1st and 2nd function works fine but if a add
a third function with const as a characteristic in passed parameters, I get compilation error. int A::sum(int, int) and int A::sum(int, int) cannot be overloaded. Just giving the code snippet for class:

class A
{
    private:
    int x;
    int y;

    public:
    int sum ( int a, int b )
    {
        cout << " Inside 1st ";
        return (a+b) ;
    }

    int sum (int a ,int b) const
    {
        cout << " Inside 2nd ";
        return (a+b) ;
    }

    int sum (const int a ,const int b)
    {
        cout << " Inside 3rd ";
        return (a+b) ;
    }

    A(){x=0;y=0;}
    ~A(){};
}; 

When I declare the normal object and make a call to sum the first function gets called and in case of a const object second sum is being called. That’s perfectly fine. But if I write 1st and 3rd function both it becomes an issue. Why so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:53:38+00:00Added an answer on May 30, 2026 at 2:53 am

    You can overload on const-ness, but only where it actually makes a difference. What do I mean “actually makes a difference”? Well, if you declare

    void foo(char *ptr);
    void foo(const char *ptr);
    

    the const matters: you are saying that one overload of foo guarantees not to modify the memory that ptr points to, and the other doesn’t. But if you declare

    void foo(int x);
    void foo(const int x);
    

    the const doesn’t matter: scalar arguments are always passed by value, so neither hypothetical variation of foo can modify the value of x in its caller, even if it wanted to. Therefore, the C++ standard says that the const is discarded, and these are two different declarations of the same function, one of which is erroneous.

    The position of the const relative to the * matters. A pointer is itself a scalar, so these are also two declarations of the same function:

    void foo(char *ptr);
    void foo(char *const ptr);
    

    because here the const qualifies the pointer itself rather than the object it points to.

    That brings us to methods: a const qualifier after the argument list on a method declaration applies to the object pointed to by this, and that can be overloaded on:

    struct A
    {
      int foo (char *ptr);
      int foo (char *ptr) const;
      int foo (const char *ptr);
      int foo (const char *ptr) const;
    };
    

    You should think of these declarations as being rewritten internally to:

    struct A {};
    int A_foo (A *this, char *ptr);
    int A_foo (const A *this, char *ptr);
    int A_foo (A *this, const char *ptr);
    int A_foo (const A *this, const char *ptr);
    

    and then everything works according to the normal rules for overloaded functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

just wanted to know how to call php script from running C code on
Just wanted to know if this function's results will always hold? private int calcHourDiff(int
Just wanted to know question w.r.t Currying If we have defined the curried function
Just wanted to know if serializing a string is equivalent to getting its bytes?
I just wanted to know how do I generate random number using J2ME CLDC
Just wanted to know if it is possible to disallow the whole site for
Just wanted to know if anyone is really using Objects and Collections in Oracle
Just wanted to know if there is a big chance to inject SQL While
Just wanted to know if overriding UITabBarController would get my app rejected? Is it
I just wanted to know your experience with using resharper. We have a very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.