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?

Where Next?

Popular General Dev topics Top

dimitarvp
What does a developer advocate do for a living? I mean, what is it that you are paid to do? I’ve seen your description below but it doesn...
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
finner
Hello devtalk ! Heroku are closing their free tier (dynos) later this month and I was wondering if you guys could recommend any alternat...
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 have an array of objects in JavaScript, and I want to sort them based on a specific property of the objects. For example, I have an arr...
/js
New
thetoaderseventytwo
I’ve been trying to dip my feet into using Unity and C# for the sake of developing games, however, I have barely any knowledge of how to ...
New
jaeyson
Is Rust still good to learn? Last time (ages ago) I heard there was changes made by the foundation. If not, is Go suitable for api and w...
New
harwind
I am working on a Python script, and you encounter an error related to the misuse of lists and tuples. Here’s a simplified version of you...
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
Tazmeen
Hello, I am new to this forum. Not really sure if this topic is relevant for this chat at all. I apologize if its not. I am trying to c...
New

Other popular topics Top

siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
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
New
New