0

i have a problem with function overloading while using const function and const object. Can someone explain why "const foo int& int" is being print instead of "foo int char&" in the following code?

struct A 
{
    void foo(int i, char& c) { cout << "foo int char&" << endl;}
    void foo(int& i, int j) const { cout << "const foo int& int" << endl;}
};

int main() {
    A a;
    const A const_a;
    int i = 1;
    char c = 'a';
    a.foo(i,i);
}

Thanks,

2
  • Did you mean a.foo(i, c)? Otherwise, why is there a c? Commented Feb 7, 2014 at 14:00
  • You are also calling on the non-const object. This seems to not match your question. Commented Feb 7, 2014 at 14:04

4 Answers 4

2

There is all clear. You called a function with two arguments of type int. There are two function candidates. One that have the first parameter of type int and the second parameter of reference to char. So to call this function the second argument shall be implicitly converted to type char &. But it is a narrowing conversion because the rank of int is higher than rank of char. So a temporary object will be created but temporary object may only be binded with a const reference. The second function does not require any conversion of its arguments. So it will be called.

Sign up to request clarification or add additional context in comments.

8 Comments

The int cannot be converted and bound by a char&. While it can be converted to a char, that would be an rvalue and you cannot bind a non-const lvalue reference to it.
@David Rodríguez - dribeas And what did I write in my post?
Sorry, I missed the 'may [only] be binded with a const reference'
@David Rodríguez - dribeas Well, maybe you will cancel you down vote mark?
I already tried, but the vote is locked. I did randomly pick another of your answers and upvoted to compensate, but I cannot remove the downvote from this one unless there are changes.
|
1

Cause void foo(int& i, int j) const is a better match, other function needs an int to char convertion.

7 Comments

As you can't bind a non-const lvalue ref to char (i.e. a char&) to an argument of type int, the first overload isn't even viable.
isn't viable at all since object a is const
@piotruś a is not const, const_a is.
no it's not Pavel, my mistake, object a itself is not const, you can invoke non-const methods on it
but does it totally ignore the fact that <i>a<\i> is not const ?
|
1

The first function cannot be called with an int as the second argument. A char& cannot be bound to an int. That means that only the second overload is an alternative.

Comments

0
  1. define function as const means you cannot change the member variables in the object
  2. non const function cannot be called by const object
  3. A character, a short integer, or an integer bit-field, all either signed or not, or an object of enumeration type, may be used in an expression wherever an integer maybe used. If an int can represent all the values of the original type, then the value is converted to int; otherwise the value is converted to unsigned int. This process is called integral promotion

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.