
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

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

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

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

What are the main stages of software development?
New

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

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

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...
New

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

Hi, I’m now investigating the complexities of Python loops, specifically the contrast between for and while loops. However, I’ve had some...
New

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

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

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

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

Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
New

Saw this on TikTok of all places! :lol:
Anyone heard of them before?
Lite:
New

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

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

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

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
Categories:
Sub Categories:
- All
- In The News (9982)
- Dev Chat (200)
- Questions
- Resources (118)
- Blogs/Talks (26)
- Jobs (3)
- Events (14)
- Code Editors (58)
- Hardware (57)
- Reviews (4)
- Sales (15)
- Design & UX (4)
- Marketing & SEO (1)
- Industry & Culture (14)
- Ethics & Privacy (19)
- Business (4)
- Learning Methods (4)
- Content Creators (7)
- DevOps & Hosting (9)
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /ash
- /vscode
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /scala
- /zig
- /debian
- /nixos
- /lisp
- /agda
- /textmate
- /sublime-text
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /spring
- /manjaro
- /django
- /diversity
- /lua
- /nodejs
- /c
- /julia
- /slackware
- /neovim