Search code examples
c++classmember

Class member function declared outside of public, protected and private


I came across some code that looked like this:

class SomeClass {
   void SomeFunction();

public:
   ~~ public members
}

There were no private or protected members of the class.

My question is; is not labelling what part of the class the 'SomeFunction' belongs to bad programming? Or does defining the function in this way implicitly assign it to some part of the class, i.e. private or protected, since it is not part of the public members?


Solution

  • In C++, the difference between class and struct is that for a struct everything is implicitly public and for a class everything is implicitly private.

    As soon as you use explicitly public:, private: or protected: the behavior is the same in both cases for the following members.

    This is true about inheritance too.
    struct A : B is a public inheritance.
    class A : B is a private inheritance.

    In your example, SomeFunction() is in the implicit part of the class, so it is considered private.

    A very well respected C++ expert considers this kind of declaration (implicitly private) as a good practice.
    ( https://howardhinnant.github.io/classdecl.html )