#include<iostream>
using namespace std;
int &fun()
{
static int x = 10;
return x;
}
int main()
{
fun() = 30;
cout << fun();
return 0;
}
Output is 30 why not 10?
please explain me the concept of Lvalue..
Thank you in advanvce.
saurav2007
0
Newbie Poster
Recommended Answers
Jump to PostHere is a slightly modified version of your code (in the function fun):
#include<iostream> using namespace std; int& fun() { static int x = 10; cout << "In fun x = " << x << endl; return x; } int main() { fun() = 30; cout << …
Jump to PostWell, I have to correct the correction because what AD said is wrong. Local variables (defined in a function) with static storage duration (with
static
keyword) are created and initialized the first time control flows over that code (i.e., the first time the function is called). MandrewP was entirely …
Jump to PostOk, to settle this, let's just look at what the C++ standard says. Here is the relevant section (that I already quoted in another recent thread where the properties of those variables is a critical part of the design) about block-scope variables (i.e., local variables) with static storage duration. …
Jump to Poststatic POD variables are initialized just like all other global POD variables. I posted assembly code to prove it.
In the case you give, where the
static
variable is initialized to a fixed value, the compiler is indeed initializing the variable at the start of the process; …
Jump to PostThat function could have been written like below, where x is an unitialized variable until it is set to the parameter.
Absolutely not! If you think that, you did not get the point, at all!
If I use the implementation you provided into this program:
All 18 Replies
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
MandrewP
60
Junior Poster in Training
mike_2000_17
commented:
Good explanation!
+14
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
MandrewP
60
Junior Poster in Training
mike_2000_17
2,669
21st Century Viking
Team Colleague
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Schol-R-LEA
1,446
Commie Mutant Traitor
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
mike_2000_17
2,669
21st Century Viking
Team Colleague
Featured Poster
deceptikon
commented:
For quoting the standard. :)
+13
Schol-R-LEA
1,446
Commie Mutant Traitor
Featured Poster
mike_2000_17
2,669
21st Century Viking
Team Colleague
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
rubberman
1,355
Nearly a Posting Virtuoso
Featured Poster
rubberman
1,355
Nearly a Posting Virtuoso
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
mike_2000_17
2,669
21st Century Viking
Team Colleague
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.