+ 2

Static variable of structure

I am unable to get how output of below code is 11? it should be 10 as per my logic. int foo() { return 10; } struct foobar { static int x; static int foo() { return 11; } }; int foobar::x = foo(); int main() { cout << foobar::x; return 0; } static variable of x is printed and it is having value as 10 returned by non static function foo(). why compiler prints 11 as output?

23rd Apr 2018, 3:31 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Answers
+ 1
hi, thank you for responding... I checked your code... I am able to understand the result... additionally, I am not making structure as static. I am still unable to understand why my posted code print 11 as output instead of 10 as output.
23rd Apr 2018, 5:23 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
ŠæŠ¾Š¼Š¾Š³ŠøтŠµ
23rd Apr 2018, 1:43 PM
Š“Š»ŠµŠ±