top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C++: Need to clear one doubt on function overloading ?

+1 vote
289 views

void fun1(int x, int y) { } and void fun1(int const x, int const y) { } can't be overloaded while
void fun1(int *x, int *y){ } and void fun1(int const *x , int const *y) { } can be overloaded. I compiled both the samples and found first set of functions can't be overloaded while the next two functions can be overloaded. I could not understand the reason behind it.

posted Sep 10, 2016 by Neelam

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

It is very common interview question and the reasoning behind it is
- First two set of messages can't be overloaded since both the function will hold constant value. That's why C++ compiler will generate error message "re-definition is not allowed"
- While in the second case, both the functions have different significance . First prototype taking arguments which are pointer to integers while second one is taking arguments pointer to constant. Please find the example for second case.

int a = 10, b = 20;
int *ptr1 = &a;
int *ptr2 = &b;
const int c = 30, d =40;
int const *ptr3 = &c;
int const *ptr4 = &d;

fun1(ptr1 , ptr2);
fun2(ptr3, ptr4);

answer Sep 10, 2016 by Harshita
...