Consider the following code snippets:
void foo(const int i)
{
std::cout << "First " << i << endl;
}
void foo(int i)
{
std::cout << "Second " << i << endl;
}
int main()
{
int i = 5;
foo(i);
}
Compilation Error:
redefinition of 'void foo(int)'
Since it constcan not be initialized with objects const, the above behavior seems reasonable. Now consider the following:
void foo_ptr(const int* p)
{
std::cout << "First " << *p << endl;
}
void foo_ptr(int* p)
{
std::cout << "Second " << *p << endl;
}
int main()
{
int i = 5;
foo_ptr(&i);
}
How can this be clear, my question is: if the two definitions fooin the first case are considered the same, then why is this not the case foo_ptrin the second case? Or, in other words, why is it constignored in the first case, and not in the second?
source
share