0

why static keyword shouldn't be re typed in .cpp file? example

//this is .h file
class A{
public:
   static void blabla();
};

//this is .cpp file
#include "Someheader"

static void A::blabla(){} //error????

I think I haven't fully understand the relation between the .h file and .cpp file, or is it just the case for static?

tbsnsa
  • 29
  • 6
  • @user4581301 sorry i was typing it wrong – tbsnsa Sep 03 '21 at 22:38
  • The `static` on the definition means the function can only be seen in one cpp file (technical term: [translation unit](https://en.wikipedia.org/wiki/Translation_unit_(programming)). If the function's existence is advertised by a header, only being accessible from one cpp file makes it pretty much useless. There may be some technical constraint, but functionally it doesn't make much sense to allow this. – user4581301 Sep 03 '21 at 22:38
  • @user4581301 that's not the usage here. – bolov Sep 03 '21 at 22:39
  • The `static` keyword has all sorts of different meanings and usage rules in C++. In the case of `static` class members, the keyword must *only* be placed on the declaration (in the header) ... and *not* on the definition (in the cpp file). – Adrian Mole Sep 03 '21 at 22:40
  • @bolov, that's not the asker's intent, I'm sure, but that should be the effect regardless. – user4581301 Sep 03 '21 at 22:41
  • It's the same with (some) other qualifiers for class member functions, like `virtual`. – Adrian Mole Sep 03 '21 at 22:42

0 Answers0