Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cpp-cheat/cpp/overload.cpp at master · Bhupesh19/cpp-cheat · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Bhupesh19
cpp-cheat
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
cpp-cheat
/
cpp
/
overload.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
120 lines (93 loc) · 3.17 KB
master
Breadcrumbs
cpp-cheat
/
cpp
/
overload.cpp
Copy path
Top
File metadata and controls
Code
Blame
120 lines (93 loc) · 3.17 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
# Overload
*/
#
include
"
common.hpp
"
//
TODO factor those dummy classes.
class
Class
{};
class
Base
{};
class
BaseProtected
{};
std::string
overload
(
int
i) {
return
"
i
"
; }
std::string
overload
(
float
i) {
return
"
f
"
; }
std::string
overload
(
int
i,
int
j) {
return
"
ii
"
; }
std::string
overload
(
float
i,
float
j,
float
k =
0
.f) {
return
"
iff
"
; }
//
OK even if return type is different all is decided at compile time.
//
int overload(int i, int j, int k){return 1;}
//
ERROR: conflict with int.
//
void overload(const int i){}
//
ERROR: cannot differentiate by output since output is used to decide if other parts of code make sense.
//
int overload(){return 0;}
//
float overload(){return 0.f;}
//
ERROR: conflict with int int
//
void overload(int i, int j=0){cout << "int int=";}
//
BAD: compiles, but is useless to give a default,
//
since when calling, caller is *forced* to give a value for j
//
or wil get `call is ambiguous` compile time error
//
because compiler cannot decide between
//
here the default arg can be usefull for a call of type float float.
//
void overload(float i, float j=1){cout << "float float=";}
//
TODO: why does this compile, that is, how not to make an ambiguous call with overload(int).
void
overloadValAddr
(
const
int
i) {}
void
overloadValAddr
(
const
int
& i) {}
void
overloadBase
(Base b) {}
void
overloadBase
(BaseProtected b) {}
int
main
() {
assert
(
overload
(
1
) ==
"
i
"
);
//
base type conversions
{
assert
(
overload
(
1
.
0f
) ==
"
f
"
);
/*
ERROR: ambiguous overload(int) overload(float)
Compiler does not know wether convert double to float or int.
*/
//
overload(1.0);
}
/*
ERROR: ambiguous
should compiler coverts to Base or BaseProtected? Both are possible via the default copy constructors.
*/
{
Class cOverload;
//
overloadBase(cOverload);
}
//
ERROR: ambiguous
//
i=4;
//
overloadValAddr(i);
/*
# volatile overload
Functions that differ by `volatile` can be overloaded:
- http://stackoverflow.com/questions/10242578/volatile-overloading
*/
{}
/*
Function pointer: decided by the typecast.
http://stackoverflow.com/questions/2942426/how-to-specify-a-pointer-to-an-overloaded-function
*/
{
//
Variable.
{
std::string
(*fi)(
int
) = overload;
std::string
(*ff)(
float
) = overload;
}
}
//
# const function overload
//
http://stackoverflow.com/questions/251159/what-is-the-use-of-const-overloading-in-c
//
http://stackoverflow.com/questions/5103543/how-does-overloading-of-const-and-non-const-functions-work/41274591#41274591
{
class
C
{
public:
int
f
() {
return
1
; }
float
f
()
const
{
return
1.5
; }
};
//
Non const.
C c;
assert
(c.
f
() ==
1
);
//
Convert variable const at compile time.
assert
(
const_cast
<
const
C&>(c).
f
() ==
1.5
);
//
Same as above but with an explicit reference.
const
C& d = c;
assert
(d.
f
() ==
1.5
);
//
Analogous but with a new const object from the start.
const
C e;
assert
(d.
f
() ==
1.5
);
}
}
You can’t perform that action at this time.