C++ | Static Keyword | Question 6
Output of following C++ program?
#include <iostream> class Test { public: void fun(); }; static void Test::fun() { std::cout<<"fun() is static\n"; } int main() { Test::fun(); return 0; } |
chevron_right
filter_none
Contributed by Pravasi Meet
(A) fun() is static
(B) Empty Screen
(C) Compiler Error
Answer: (C)
Explanation: The above program fails in compilation and shows below error messages.
[Error] cannot declare member function ‘void Test::fun()’ to have static linkage [-fpermissive]
In function ‘int main()’:
[Error] cannot call member function ‘void Test::fun()’ without object
If the static function is to be defined outside the class then static keyword must be present in function declaration only not in the definition outside the class.
Following program is now correct.
#include <iostream> class Test { public: static void fun(); }; void Test::fun() { std::cout<<"fun() is static\n"; } int main() { Test::fun(); return 0; } |
chevron_right
filter_none
Recommended Posts:
- C++ | Static Keyword | Question 1
- C++ | Static Keyword | Question 2
- C++ | Static Keyword | Question 5
- C++ | Static Keyword | Question 3
- C++ | Static Keyword | Question 4
- Static Keyword in C++
- Comparison of static keyword in C++ and Java
- Output of Java Programs | Set 48 (Static keyword)
- Understanding "static" in "public static void main" in Java
- Difference between static and non-static method in Java
- Difference between static and non-static variables in Java
- Internal static variable vs. External static variable with Examples in C
- C++ | const keyword | Question 1
- C++ | const keyword | Question 5
- C++ | const keyword | Question 2

