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)?
Related
This question already has answers here:
How do I specify a pointer to an overloaded function?
(6 answers)
Closed 3 years ago.
How do we create a std::function object for overloaded member functions in a class? std::function object can be created for a non-overloaded member function as shown.
Sample code is attached as shown below
#include<iostream>
#include<functional>
class X
{
public:
X(){}
virtual ~X(){}
void foo1(char c)
{std::cout<<"X::foo1(char c)\n";}
void foo2(char c)
{std::cout<<"X::foo2(char c)\n";}
void foo2(int i)
{std::cout<<"X::foo2(int i)\n";}
}x;
int main()
{
std::function<void(X*,char)> f1=&X::foo1;
f1(&x,'a');
/* //The following code doesn't work
std::function<void(X*,char)> f2=&X::foo2;
f2(&x,'a');
*/
return 0;
}
Following error is given :
conversion from '' to non-scalar type 'std::function' requested
You need to be explicit about the signature of the overloaded function you intend to use:
std::function<void(X*,char)> f2 = static_cast<void (X::*)(char)>(&X::foo2);
As an alternative, use a lambda:
std::function<void(X*,char)> f3 =
[](X *instance, char c){ return instance->foo2(c); };
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:
Function pointer vs Function reference
(4 answers)
Closed 6 years ago.
int g()
{
return 0;
}
int f1(int(fn)())
{
return fn();
}
int f2(int(*fn)())
{
return fn();
}
int main()
{
f1(g);
f2(g);
}
The code above can be sucessfully compiled.
I just wonder:
What's the difference between int f(int(fn)()) and int f(int(*fn)())?
Why does both of them are legal in C++?
int f(int(fn)()) and int f(int(*fn)()) are the same thing. A function takes a function pointer as parameter.
When we pass the name of a function as parameter, the function name is automatically converted to a pointer. So
int f(int(fn)()) {}
int f(int(*fn)()) {} // redefinition error
This question already has answers here:
"const T &arg" vs. "T arg"
(14 answers)
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 7 years ago.
// const objects
#include <iostream>
using namespace std;
class MyClass {
int x;
public:
MyClass(int val) : x(val) {}
const int& get() const {return x;}
};
void print (const MyClass& arg) { // Need to understand this line
cout << arg.get() << '\n';
}
int main() {
MyClass foo (10);
print(foo);
return 0;
}
I am new to C++. Need to understand what are the parameters passed in print function. If this is address then why are we passing foo is print function call.
The & in void print (const MyClass& arg) that arg is passed by reference. It is C++s way to make pointers and things a little bit easier.
References allow you to manipulate a variable inside of a function and make the results visible on the outside too. So a bit like pointers. But you don't need to explicitly get the address of the variable to do that.
The const statement is a way to prevent the described behavior. const forbids the manipulation of arg inside print.
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?