C++类中访问说明符外成员变量的权限及设计疑问
Great question—let’s unpack this clearly, since it’s a common point of confusion with C++ class member access rules.
First, let’s confirm the core rule from the C++ standard:
For a
classtype (as opposed tostruct), any member that appears before the first access specifier (public,private,protected) has private access by default. This applies to both static and non-static members equally.
So in your Card class, the static variables m_scale, m_width, and m_height—which are declared before the public specifier—are absolutely private. They have the same access level as the members in the explicit private block later in the class. This is fully compliant with all C++ standards (C98 through C20 and beyond).
Why might someone write code this way?
There’s no special technical reason for this placement—it’s almost always a matter of coding style or organization:
- Grouping static members: Some developers prefer to declare all static members first, regardless of access level, to keep them grouped together. This can make it easier to scan for class-wide shared variables at a glance.
- Legacy or personal habit: Older codebases or individual developers might have established patterns where they handle static declarations before splitting into access blocks. It’s not the most common modern practice, but it’s syntactically valid.
A note on best practices
While this code is legal, it’s generally better to explicitly place these static variables inside the private block (alongside the other private static members like m_symbolBmap) for readability. Explicit access specifiers remove any ambiguity for other developers reading your code. Here’s how that might look:
class Card { friend class FortyApp; private: // Group all private static members together static double m_scale; static int m_width,m_height; static wxBitmap* m_symbolBmap; static wxBitmap* m_pictureBmap; // Private non-static members Suit m_suit; int m_pipValue; // in the range 1 (Ace) to 13 (King) SuitColour m_colour; // red or black bool m_status; WayUp m_wayUp; public: Card(int value, WayUp way_up = facedown); virtual ~Card(){}; void Draw(wxDC& pDC, int x, int y); static void DrawNullCard(wxDC& pDC, int x, int y); // Draw card place-holder void Erase(wxDC& pDC, int x, int y); void TurnCard(WayUp way_up = faceup) { m_wayUp = way_up; } WayUp GetWayUp() const { return m_wayUp; } int GetPipValue() const { return m_pipValue; } Suit GetSuit() const { return m_suit; } SuitColour GetColour() const { return m_colour; } static void SetScale(double scale); static int GetHeight() { return m_height; }; static int GetWidth() { return m_width; }; static double GetScale() { return m_scale; }; };
Final takeaway
Your original code is 100% compliant with C++ standards, and those static variables are indeed private. The unusual placement is just a stylistic choice—there’s no hidden technical implication. For clarity, though, explicitly grouping private members (static and non-static) together is a good practice to adopt.
内容的提问来源于stack exchange,提问作者DermotA

