+ 3

why the output here equals 11 not 10 ?

#include<iostream> int foo() { return 10; } struct foobar { static int x; static int foo() { return 11; }}; int foobar::x = foo(); int main() { std::cout << foobar::x ; }

10th Jul 2022, 3:18 PM
Aly Alsayed
Aly Alsayed - avatar
4 Answers
+ 3
static int foo() forbids int foo() there, when it is there... You can call only static member directly or through reference of class name. So there foo() refers to static int foo() of struct foobar first..
11th Jul 2022, 1:34 PM
Jayakrishna 🇼🇳
+ 3
Don't no but when we comment out the struct foo().. Then output is 10.. https://stackoverflow.com/questions/23329382/function-and-struct-having-the-same-name-in-c
10th Jul 2022, 5:10 PM
Mihir Lalwani
Mihir Lalwani - avatar
+ 3
Mihir Lalwani when i use std::cout <<foo(); // output 10 std::cout <<foobar::foo(); //output 11 I'm fine with these two statments but i can't understand this std::cout << foobar::x ; // ?? why it outputs 11 ?
10th Jul 2022, 8:37 PM
Aly Alsayed
Aly Alsayed - avatar
+ 1
Aly Alsayed check above link...
11th Jul 2022, 1:35 PM
Mihir Lalwani
Mihir Lalwani - avatar