-4

I have a pre-existing source code in C similar to below.

bool getFlag(int param)
{
  static bool flag = false;
  if(param == 1)
    flag = true;
  return flag;
}

I have written the C++ version of the same as below.
class MyClass
{
  public:
    static bool getFlag(int param)
    {
      if(param == 1)
        flag = true;
      return flag;
    }
  private:
    static bool flag;
};

What is the difference between the above two code snippets? Does the C++ code above has advantage over the C code in any ways?

  • Note the C code is also perfectly good C++ code. –  Aug 28 '17 at 20:47
  • Why did you do this? The original function is fine as it is. Your class with a single static function and a static member doesn't add any value, and unless you are being paid by the line of code, there is no reason to do this. Please note, the same question phrased differently (would is the difference between those two approaches in the C++, for example) would have a valid answer. – SergeyA Aug 28 '17 at 20:47
  • 2
    Possible duplicate of [C++, What is difference between static local variable(method) vs global(file) variable?](https://stackoverflow.com/questions/31578571/c-what-is-difference-between-static-local-variablemethod-vs-globalfile-va) – rustyx Aug 28 '17 at 20:50
  • A `static` variable in a class means that there is one instance shared by all class instances and descendants. A `static` variable in a function is only visible by the function and the variable is persistent after execution leaves the function. A `static` method in a class does not require an instance of the class to execute the function; similar to a free standing function. – Thomas Matthews Aug 28 '17 at 20:59
  • @Neil Butterworth - "Note the C code is also perfectly good C++ code" - I wouldn't go that far. Sure, in *many* cases that is true. But there are also many cases where the exact same code has different semantics under C vs C++. They reaally *are* different languages by now and the common sub-set that's both syntactically and semantically identical has shrunken quite a bit since C89/C++98 compared to C11 and C++17. – Jesper Juhl Aug 28 '17 at 21:22
  • @Jesper For example (in this case)? If I were to write this function in C++11 or later, the above is exactly how I would write it. –  Aug 28 '17 at 21:23
  • @Neil Butterworth One trivial example: the literal `'x'` is of type `int` in C. In C++ it is of type `char`. Btw; I was *not* talking about "this code", but instead about your blanket statement that "C code is also perfectly good C++ code" which is simply not always true. – Jesper Juhl Aug 28 '17 at 21:27
  • @Jesper Did you read where I said "in this case"? Of course I know there are major differences between C and C++. And as usual you have edited my initial comment, eliding a "the", i.e. meaning "this specific code". I won't be replying to any more of your unhelpful comments. –  Aug 28 '17 at 21:29
  • @Neil Butterworth I didn't mean to elide anything. I just wanted to say that C and C++ can impose quite different meaning on the same source. Sorry if you feel I quoted you badly - I really didn't mean to. Just wanted to make one (fairly simple) point. I'm sorry you seem to feel that this is something I do a lot - I do apologize for that. That has never been my intention. – Jesper Juhl Aug 28 '17 at 21:33

2 Answers2

0

Its somewhat similar. Anyone who interacts with any instance of your class MyClass will interact with the same variable flag.

The same is true of your function. Any caller will interact with the same static variable.

However, there is definitely some ambiguity in how they behave in a multi-threaded environment depending on your compiler (are you compiling pure c functions or a mixed c/c++ with a new compiler?)

Basically, initialization wasn't thread safe before C++11, and you'd get data races if two threads reached the initialization (or subsequent modification) of the local static variable. This existed all the way up until Visual Studio 2015 on the Microsoft side.

As such, on modern compilers, C++ behaves differently.

https://stackoverflow.com/a/11711991/128581

If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.

Josh
  • 12,262
  • 2
  • 38
  • 46
0

Without the whole context it's hard to say which is better, it's just a matter of abstraction. Even at the assembly level you can't tell the difference, the static variable goes to the .BSS segment in both cases and the logic is exactly the same, because your method is static(and assuming it's the only method you have and call), it doesn't make use of the hidden "this" argument or have a constructor call at any point. You can only tell the difference by compiling the code in debug mode and interpret the mangled names generated.

savram
  • 491
  • 3
  • 15