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
AstonJ
Stopwords are words that you normally filter for things like search queries, such as ‘as’, ‘because’ etc - there are a few online, but I ...
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
Girtyp
Hi everyone. Getting right to my question, I have recently thought about implementing payment by installment feature in my website. Who k...
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
harwind
I have an array of strings in JavaScript, and I need to convert it into a single string with specific delimiter characters between the el...
/js
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’m considering learning new languages to expand my programming skills in 2023. While aware of popular choices, I seek advice on striking...
New
harwind
Hi, Take a riveting look at exception handling in Java programming, including the complicated dance between try-catch blocks, checked an...
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

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
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
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1139 25582 756
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
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
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New