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
Related
This question already has answers here:
How can I pass std::unique_ptr into a function
(8 answers)
Closed 3 years ago.
How do I pass the unique_ptr in the below code? This code does not compile.
int abc(int*& a)
{
return 1;
}
int main()
{
std::cout<<"Hello World";
std::unique_ptr<int> a(new int);
abc(a.get());
return 0;
}
You may pass it by const reference or reference for example:
int abc(const std::unique_ptr<int>& a)
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:
Assigning value to function returning reference
(7 answers)
Closed 6 years ago.
What is the meaning of saying
fun()=30;
count<<fun();
Function definition is:
int &fun()
{
static int x = 10;
return x;
}
int& fun();
The return type of fun() is an integer reference.
fun() = 30
assign the value 30 to the integer referenced by the return value of fun().
Which integer?
int &fun()
{
static int x = 10; // <-- this one
return x;
}
This question already has answers here:
C++ Overload Static Function with Non-Static Function
(5 answers)
Closed 7 years ago.
I was looking into a function overloading problem listed below and found that the following code doesn't compile.
#include<iostream>
class Test {
static void fun(int i) {}
void fun(int i) {}
};
int main()
{
Test t;
return 0;
}
My understanding was that member functions when compiled implicitly have an extra parameter, a pointer to the object in the compiled function. I am not sure what happens to the static functions. Now to figure out what the compiler was doing I tried running g++ -fdump-tree-all failed_overload.cxx and I got the files listed below:
failed_overload.cxx.001t.tu
failed_overload.cxx.002t.class
failed_overload.cxx.003t.original
failed_overload.cxx.004t.gimple
failed_overload.cxx.204t.statistics
I looked into gimple output and found the following:
**
static void Test::fun(int) (int i)
{
GIMPLE_NOP
}
void Test::fun(int) (struct Test * const this, int i)
{
GIMPLE_NOP
}
**
It seems like the static function just has the int parameter but the member function has the extra this parameter. If that is the case why is the compilation failing and why cant we overload the static function with the same signature.
If you had both static and non-static functions taking the same set of parameters, then in a call from a method of the class (non-static) it would be impossible to distinguish whether the programmer wants to call static or non-static function. Example:
#include<iostream>
class Test {
static void fun(int i) { std::cout << 2*i; }
void fun(int i) { std::cout << i; }
void otherFunc() {
fun(3); // Ambiguity: is static or non-static function intended?
}
};
int main()
{
Test t;
t.otherFunc();
return 0;
}