1

Consider following program.

#include <iostream>
void fun(const char a[]) {
    std::cout<<"function 1\n"; 
    std::cout<<a<<'\n';
}
void fun(char *a) {
    std::cout<<"function 2\n"; 
    std::cout<<a<<'\n';
}
int main() { 
    fun("string");
}

What should I change in the program so that second function gets called instead of first one?

1 Answer 1

1

Well you are passing in a const char * when you pass in "string"

what you could do is declare a char * and set it to something and then pass it in and it will go to the second function.

"string" is a const char* which will go to the first function

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

3 Comments

@meet To do what exactly? Again, "string" is const, you can´t change that.
@deviantfan: Ok, I understood. you mean to say modifying string literal is UB in C++ because it is stored in read only memory. right?
@meet While that´s true, why is this relevant? First, it was a question about overloading (only), now you actually want to modify it ... Again, tell us what you really want, not how do you think you could do it.

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.