20

While implementing a class for creating/updating boxes on the screen, I wanted to add a static member function that makes sure no currently visible boxes overlap (taking its information from a static pointer array to all currently visible boxes)

My initial code had the following structure:

class Box
{
public:
    // ...
    static void arrangeOverlappingBoxes();
};

static void Box::arrangeOverlappingBoxes()
{
    // ...
}

I was quite surprised that this generated an error C2724: 'static' should not be used on member functions defined at file scope.

With some trial, google and error, I figured out that my function definition should lose the keyword static, i.e. it should be

void Box::arrangeOverlappingBoxes()
{
    // ...
}

Yet I have no clue what the rationale behind this could be. It appears to be so asymetric and confusing to have a different function header for its declaration in the class definition and its own definition. Is there any reason for this?

manlio
  • 18,345
  • 14
  • 76
  • 126
mr_T
  • 2,571
  • 3
  • 22
  • 39

1 Answers1

15

Your class definition (in the header file) will provide the function with whatever propreties are necessary :

  • static
  • inlined
  • virtual

Considering that every further object will look at your class definition using the .h then it makes sense that these properties to be defined there.

Furthermore, each function from the class will mentain it's property in the derived classes (for example you need to declare the destructor virtual only in your base class, every subsequent inheritance will take the destructor as virtual).

It makes no sense to redeclare these properties in your implementation body .

Having to declare function proprieties in both .h and .cpp files would actually lead to allot of problems. Imagine this scenario : you declare a function as virtual in a .h file, and as static in the .cpp file. What will the compiler make that function ? virtual or static ? (or more likely a compile error , but the compiler error will just urge you to match in your .cpp file the declaration in the header. You cannot overload a function according to "static" or "virtual").

MichaelCMS
  • 4,703
  • 2
  • 23
  • 29
  • 2
    Thank you for your answer but I still don't get the point yet. The compiler could be programmed just to compile in case the attributes are the same, just like the parameters should be equal (In fact, Java syntax requires it to be this way). So why would you say it makes no sense? In my humble opinion, it makes a lot of sense: if you just look in your cpp file (assuming your class definition is in the header), you see immediately that the function is static and does, for instance, not have a this pointer without having to look in your header file. – mr_T Sep 23 '14 at 08:08
  • Think of it this way : what problems will arise if you were allowed to specify the type of function in both .cpp and .h ? You will see that the compiler would actually force you in your .cpp to implement the function with the same property as in the header file, otherwise you will get an unresolved external symbol linkage error (remember that all external classes link with the definition of the function from the header). So it would be conterproductive to have to declare in the cpp file this property as well. C++ is complex enough as it is, no need for extra challenges ;). – MichaelCMS Sep 23 '14 at 08:12
  • Regarding your edited comment : you can either use naming conventions or comments to underline "static" or such attributes. I personally look in header files when it comes to method types (you have go do declaration and go to definition )... Maybe this is a matter of style – MichaelCMS Sep 23 '14 at 08:14
  • 2
    Thank you, naming conventions will indeed be how I will deal with it but I'm however not yet fully convinced of the counterproductivity of requiring equal signatures (name, parameters and attributes) in declarations and definitions. The fact that it is implemented differently this way in more recent languages tempts me to regard this as a C++ quirk. – mr_T Sep 23 '14 at 08:19
  • What recent langauges allow you to declare inside a class a function as beeing static and outside the class the function as beeing non-static (while keeping the same function of course) ? – MichaelCMS Sep 23 '14 at 08:24
  • none, of course. I meant that recent language require to put the same attributes (static etc.) in declaration and definition, unlike c++. – mr_T Sep 23 '14 at 08:47
  • The static keyword is also used for names, which are restricted to file scope. It is a poor man's namespace. I think this comes from C. – Sebastian Nov 23 '21 at 20:07