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;
}
Related
This question already has answers here:
C++ return value without return statement
(6 answers)
C++ - Program returning values without return statement [duplicate]
(2 answers)
Return value from writing an unused parameter when falling off the end of a non-void function
(1 answer)
Closed 9 months ago.
Why p() fucntion return a value? which is returned by call() fucntion .
when i called function p() . it has not any return statement , but autometicaly return value of call() fcuntion .
int call(int x)
{
return x;
}
int p(int k)
{
call(4);
}
void solve()
{
cout<<p(9)<<endl; //output is x
}
int main()
{
solve();
}
This question already has answers here:
When are static and global variables initialized?
(4 answers)
Closed 3 years ago.
If I have a variable declared with storage ie int x; and initialize it with a call to a constexpr function will it have the value determined before any code in main starts executing.
constexpr int get_value() { return 5;}
int x = get_value();
int main() {
return x;
};
In C++20, you have constinit for that:
constexpr int get_value() { return 5;}
// Still mutable, not constexpr but
// initialized with a value at compile time.
constinit int x = get_value();
int main() {
return x;
};
This question already has an answer here:
Why can in-class initializers only use = or {}? [duplicate]
(1 answer)
Closed 5 years ago.
This is the example from Effective modern c++.
class Widget {
…
private:
int x{ 0 }; // fine, x's default value is 0
int y = 0; // also fine
int z(0); // error!
};
direct initialization using ()
Inside the class treats the below
int z(0);
As a function as and expects parameter .As result error
Expected parameter declarator
alternatively can be done
class Widget {
private:
int x{ 0 };
int y = 0;
int z;
public:
Widget():z(0){}
};
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