0

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
*/

2 Answers2

0

You're experiencing something called Undefined Behavior. This happens when you break a rule. In programs with Undefined Behavior (UB), any outcome is considered valid. And we mean "any". Deleting your harddisk? Perfectly valid outcome. Working as intended? Could also happen. Working as intended, until your boss looks? You might start noticing a pattern here.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

I think that this question's answer will help you understand what's going on:

"Why does flowing off the end of a non-void function without returning a value not produce a compiler error?"

In a nutshell, your compiler does not check for the lack of return statement because it's hard, and the reason b gets set to 5 is probably due to the residual stack state of the method call.

Ian Neal
  • 113
  • 5
  • 1
    Thanks for contributing, but on StackOverflow the policy is to close duplicate questions. – MSalters Jul 26 '17 at 21:17
  • @MSalters: I think the question is a little more than the supposed duplicate, because the OP wants to know why he receives the expected result. I was just explaining what probably happened on the stack and why this is purely coincidentally, but couldn't post that answer. – bjhend Jul 26 '17 at 21:29
  • BTW, I didn't downvote this answer. – bjhend Jul 26 '17 at 21:32
  • @bjhend: While we're at the subject of SO etiquette, questions and answers have their own comments, and I don't think you intended to comment on the answer here. But note that the accepted answer on the linked question also mentions _why_ this can happen. (last paragraph) – MSalters Jul 26 '17 at 21:36
  • @MSalters: Sorry, that I didn't find the right place to comment on the duplication mark. At least it was a bit frustrating to put a little work into an answer and then find a disabled post button, although I understand and appreciate in general the prevention of duplicates on SO. – bjhend Jul 26 '17 at 21:44
  • @bjhend: I can relate - I posted an answer myself, then saw Ian's contribution, and realized it was a duplicate. – MSalters Jul 26 '17 at 21:50