harwind

harwind

Function Overloading In C++ - why is const disregarded in the first example but not in the second?

Consider the following bits of code:

void foo(const int i) // First foo
{
   std::cout << "First " << i << endl;
}

void foo(int i)       // Second foo
{
   std::cout << "Second " << i << endl;
}

int main() 
{
   int i = 5;
   foo(i);      
}

Redefinition of ‘void foo(int)’ causes a compilation error.

Because consts can be initialised with non-const objects, the preceding behaviour appears to be logical. Consider the following:

void foo_ptr(const int* p)  // First foo_ptr
{
   std::cout << "First " << *p << endl;
}

void foo_ptr(int* p)        // Second foo_ptr
{
   std::cout << "Second " << *p << endl;
}

int main()
{
   int i = 5;
   foo_ptr(&i);             // Second foo_ptr gets called; prints 'Second 5'
}

As you may have guessed, my query is if the two definitions of foo in the first example are regarded equivalent, why isn’t the same true for foo ptr in the second? In other words, why is const disregarded in the first example but not in the second?

Popular General Dev topics Top

Jase
Do they publish their stacks or is it mostly a case of guesswork? Twitter facebook instagram snapchat tiktok google et all. Used to be we...
New
AstonJ
I really like our #general-developer-forum:in-the-news section and am wondering whether we could automate some of the cross-posting of th...
New
brennan
Trying to understand recursion in Elixir. Sometimes it is simple based on the problem, sometimes it is hard. Any suggestions on how to le...
New
New
sona11
How can I apply a modified date and time to a variable? This is what I get when I execute the following query in SQL Server Mgmt Studio: ...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
harwind
I’m working on a web application where users can sign up with their email addresses. To ensure data integrity, I want to implement client...
/js
New
harwind
Given an array of integers, find the length of the longest increasing subsequence. A subsequence is a sequence that can be derived from a...
New
harwind
Hi, I’m now investigating the complexities of Python loops, specifically the contrast between for and while loops. However, I’ve had some...
New
GTX
Hello everyone! I am not a developer, just wanna know if it’s possible for someone with no skills to learn how to reverse hack my hackers.
New

Other popular topics Top

DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
PragmaticBookshelf
Design and develop sophisticated 2D games that are as much fun to make as they are to play. From particle effects and pathfinding to soci...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1134 25392 752
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New