In the class, we have a static member function called set_value. This function, since static allows it, can be accessed by the main() given the scope resolution by class Something. Its return type is int, but the function does not return anything. This is fine. However, how does the compiler know to assign this value to b in the call statement made in main()?
#include <iostream>
using namespace std;
class Something{
private:
static int value;
public:
static int set_value (int x)
{
value = x;
// return statement missing? But still works!
}
};
int Something::value = 1;
// since no object of something is created we call
// the constructor of the data type in the object
int main()
{
int b;
cout << "Success!" << endl;
b = Something::set_value(5);
cout << "The Value of b is " << b << endl;
return 0;
}
I get the following output
/*
Success!
The Value of b is 5
*/