Home > Backend Development > C++ > Why Can't Non-Const Static Members and Arrays Be Initialized Within a C Class Definition?

Why Can't Non-Const Static Members and Arrays Be Initialized Within a C Class Definition?

Mary-Kate Olsen
Release: 2024-12-12 21:07:18
Original
521 people have browsed it

Why Can't Non-Const Static Members and Arrays Be Initialized Within a C   Class Definition?

Why Static Data Members and Arrays Cannot Be Initialized in Class Definitions

Non-Const Static Members

The C standard restricts the initialization of static data members within class definitions to only static constant integral or enumeration types.

class A {
  static const int a = 3;  // Allowed
  static int b = 3;       // Error: Non-const static member cannot be initialized in class definition
  static const int c[2] = {1, 2};  // Allowed
  static int d[2] = {1, 2};       // Error: Non-const static array cannot be initialized in class definition
};
Copy after login

This limitation is due to the way static data members are handled by the compiler. Every static data member must have a unique definition, and if initialized in the class definition, it would create multiple definitions when the class is included in multiple translation units.

Static Arrays

Static arrays, even constant arrays, cannot be initialized in class definitions because their size cannot be determined at compile time. The compiler requires the size of an array to be known before it can allocate memory for the array.

Workaround Using "enum Trick"

To initialize static arrays in class definitions, a workaround using the "enum trick" can be employed:

class A {
  static const int a = 3;
  enum { arrSize = 2 };
  static const int c[arrSize] = {1, 2};
};
Copy after login

Standard Compliance Reasons

The standard's restriction on initializing non-const static members stems from the need to prevent ambiguous definitions and to ensure unique member definitions across translation units.

As for static arrays, the C language design has prioritized compile-time efficiency, requiring the array size to be known at compile time to optimize memory allocation. However, C 11 introduced the concept of constant expressions, allowing the initialization of const data members with values determined at compile time, which may provide more flexibility in future C versions.

The above is the detailed content of Why Can't Non-Const Static Members and Arrays Be Initialized Within a C Class Definition?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template