3

In C header file:

const int AvarInC = 1;

Now in C++ file:

class Cpp
{
   public:

   static const int PublicAvar = 1;

   private:

   static const int Avar = 1;
};

while i have: const int AvarInC = 1; in my C headers OR static const int AvarInC = 1; in my C headers.

Will it be the same thing? Is the first one without keyword static, implicitly static?

  • The variable `Avar` cannot be accessed from outside the scope of `Cpp`. `PublicAvar` can be accessed from anywhere as `Cpp::PublicAvar`. – Bart van Nierop Apr 01 '14 at 12:39
  • Thanks Bart van Nierop. You are right about PublicAvar. But i think we should have all our variables in Private scope. So i asked it. – learning fellow Apr 01 '14 at 12:41
  • BTW, `class Cpp` makes me shiver. I hope this is a contrived example, not your way of mimicking Java, attempting to put your `main()` inside a class... – DevSolar Apr 01 '14 at 12:45
  • This is such an ambiguous question. It will be hard to answer. – Engineer2021 Apr 01 '14 at 12:49

4 Answers4

2

It is similar. Of course, you can use the C way in C++ as well.

But the specific C++ way (a class member) also differs in visibility and scope. Only code that is part of class Cpp (or a friend) can access Avar. The PublicAvar is somewhere in between; anyone can access it, but they need to refer to it as Cpp.PublicAvar.

The keyword static has two distinct meanings in C++. Sometimes it means the storage class (as in your example) - that is, "this is not a per-instance member". In this respect, a variable which is not a member of a class at all is obviously static without having to declare that.

Other times, static may refer to link-time visibility. A static symbol is only valid inside its own compilation unit. That's the original C meaning. It does not come into your example especially because we are assuming that your code goes in a header file, and is therefore present in multiple compilation units. Marking a non-member variable as static would result in a number of copies of the variable in the program (one per compilation unit) which would not hurt for a const variable, but generally it could lead to very confusing program behavior.

Jirka Hanika
  • 13,301
  • 3
  • 46
  • 75
  • while i have: const int AvarInC = 1; OR static const int AvarInC = 1; in my C headers. Will it be the same thing? Is the first one without keyword static, implicitly static? – learning fellow Apr 01 '14 at 12:44
  • @learningfellow: Please read the linked possible duplicate. No, `static` is never implicit, and having a `static` variable in a C header is almost certainly **not** what you want. – DevSolar Apr 01 '14 at 12:54
  • tried to expand the answer to better cover the last two comments – Jirka Hanika Apr 01 '14 at 12:58
1

If I understand you correctly, no they aren't quite the same. Since you are putting the C one in a header file, I'm assuming it will be a global constant, therefore there will really only be one copy of it anyways. It won't need to be recreated each time (since that what static will do.

However, the one in the C++ file is inside a class and static, so every time that class is instantiated into an object all instances will share the same copy of the PublicAvar variable and it won't get recreated with each object.

Though, I have a feeling you knew what static does. So technically no, they don't do the same thing and aren't the same thing...but they do act similarly.

I'm not an expert, so I could be wrong but I believe this is right. Hope it helps.

Josh Braun
  • 490
  • 2
  • 16
-1

"The same" is quite ambiguous. Both will be treated as compile-time constant. You can (should) not change them at runtime and the compiler can decide to use this information to optimize your code.

Danvil
  • 22,240
  • 19
  • 65
  • 88
  • 1
    I think this is the first time I've come across the term "compile-time variable" for a constant. It's certainly correct in a way, but... weird. Not my downvote, though. – DevSolar Apr 01 '14 at 12:46
  • Changed it to "compile-time constant". It should indicate that the compiler knows the value of the constant. In opposite to `const int a = foo();` (and foo reads a file) where the value is not known at compile time. – Danvil Apr 01 '14 at 12:51
-1

No, they are not same. They are both variables are initialized at compile time. But static variable can be accessed without creating any object of a class, but non static variable only exists within an object. i.e, the static variable takes common space in the memory for any number of objects created for it, but not in case of the other one.

deb_rider
  • 570
  • 2
  • 12