This question already has answers here:
Overloading by return type
(11 answers)
Overload a C++ function according to the return value
(16 answers)
Closed 8 years ago.
In C++ can you have overloaded functions with the same parameter types but different return types?
The idea of overloaded functions is this
int foo (int x, int y);
int foo (double x, double y);
But will this work?
int foo (int x, int y);
double foo (int x, int y);
if the second way works, how does the compiler know which function to choose?
Related
This question already has answers here:
How do I remove code duplication between similar const and non-const member functions?
(21 answers)
Elegant solution to duplicate, const and non-const, getters? [duplicate]
(8 answers)
Avoid literally duplicating code for const and non-const with auto keyword?
(3 answers)
Closed 6 days ago.
class nums
{ public:
int sum(int a,int b) const
{
return a+b;
}
int sum(int a,int b)
{
return a+b;
}
};
int main()
{
const nums a;
a.sum(5,10);
nums b;
b.sum(1,2);
}
I am trying remove two functions for const objects and non const objects replace with one function using casting operators.
You can use int sum(int a,int b) const even if the object isn't marked as const. See this answer.
So, the following code would compile:
class nums
{
public:
int sum(int a,int b) const
{
return a+b;
}
};
int main()
{
const nums a;
a.sum(5,10);
nums b;
b.sum(1,2);
}
Link to Compiler Explorer.
This question already has answers here:
Prevent implicit conversion for primary data types C++
(2 answers)
Closed 1 year ago.
I'm trying to make function that gets only int parameters. So when other types(double, char, bool) are included I used delete to exclude that like this code:
#include <iostream>
using namespace std;
int add_int(int x, int y){
return x + y;
}
int add_int(double, double) = delete;
int add_int(int, double) = delete;
int add_int(double, int) = delete;
int add_int(bool, int) = delete; // need much more code.. very inefficient
int main(void){
cout << add_int(1, true) << endl; // error should happen
}
But I think this is so inefficient. Is there any method like 'only int possible' or 'exclude double, char, bool(I mean input only one parameter)'?
Yes, you can use a function template that deletes everything.
template<class X, class Y>
int add_int(X, Y) = delete;
Now, when the compiler is resolving a function call, it will select the template unless there is a function overload where implicit type conversion is not required. An overload that requires no conversions will be selected in preference to a template function.
More information here: Overload resolution
If you like, you can also make your implementations part of this template by adding specializations:
template<>
int add_int(int x, int y)
{
return x + y;
}
This question already has answers here:
Use of 'const' for function parameters
(31 answers)
Why does a function declaration with a const argument allow calling of a function with a non-const argument?
(4 answers)
Closed 4 years ago.
I wander why c++ allows to declare this?
void f(int x);
int main()
{
int x;
const int y=0;
f(x);
f(y);
}
void f(const int x)
{
}
Why compiler allows you declare function with one signature, but define with another (cv qualifier added)?
This question already has answers here:
assignment operator within function parameter C++
(3 answers)
Closed 5 years ago.
Here is a modified code from geeks for geeks.
#include<iostream>
using namespace std;
class Test {
int value;
public:
Test(int v = 0) {value = v;}
int getValue() {return value;}
};
int main() {
Test t(20);
cout<<t.getValue();
return 0;
}
What does parameter in function Test(int v=0) means?
That is default value for v parameter, which is the v variable will use that value if there is no value passed
This question already has answers here:
C++: What is the size of an object of an empty class?
(17 answers)
Closed 9 years ago.
When we use sizeof operator on object of that class which do not have any data members. Then what would be the output? Why this always output 1??
class Abhi
{
public :
int sum (int, int);
int avg (int);
};
int Abhi:: sum(int a, int b)
{
float c;
c=a+b;
return 0;
}
int main()
{
Abhi abh;
int c;
c= sizeof(abh);
cout<<c;
}
According to the standard, each instance of a class must have a unique address. This is usually implemented by giving an "empty" class (no data members) a minimum size, as if there is a dummy char inside. This is why sizeof gives 1 and not 0.