The difference between const and static readonly in C++

  • 2020-05-17 06:08:13
  • OfStack

The difference between const and static readonly in C++

As we all know, const and static readonly are really similar: access by class name rather than object name, read-only in the program, and so on.
It can be mixed in most cases.

The essential difference between const and const is that the value of const is determined at compile time, so it can only be specified through a constant expression at declaration time. static readonly is evaluated at run time, so it can also be assigned via a static constructor.

With this distinction in mind, it is not difficult to see whether static readonly and const are interchangeable in the following statements:


1. static readonly MyClass myins = new MyClass();
2. static readonly MyClass myins = null;
3. static readonly A = B * 20;
  static readonly B = 10;
4. static readonly int [] constIntArray = new int[] {1, 2, 3};
5. void SomeFunction()
  {
   const int a = 10;
    ...
  }

1: you can't change it to const. The new operator is required to execute the constructor, so it cannot be determined at compile time
2: you can change it to const. We also see that constants of type Reference (except for String) can only be Null.
3: you can change it to const. We can say very clearly at compile time that A is equal to 200.
4: you can't change it to const. It's the same thing as 1, although it does seem like an array of 1,2,3 is a constant.
5: it cannot be replaced with readonly. readonly can only be used to modify field of a class. It cannot modify local variables or other class members such as property.

Therefore, static readonly can be used for places that should be constants in nature but cannot be declared using const. For example, the example given in the C# specification:


public class Color
{
  public static readonly Color Black = new Color(0, 0, 0);
  public static readonly Color White = new Color(255, 255, 255);
  public static readonly Color Red = new Color(255, 0, 0);
  public static readonly Color Green = new Color(0, 255, 0);
  public static readonly Color Blue = new Color(0, 0, 255);

One thing to note about static readonly is that for one static readonly Reference type, it is just restricted to not being able to assign (write). Access to its members remains unrestricted.


public static readonly MyClass myins = new MyClass();
 ... 
myins.SomeProperty = 10; // normal 
myins = new MyClass();  // Error, the object is read-only 

However, if MyClass in the example above is not one class but one struct, then both of the following statements will fail.


 private byte red, green, blue;
  public Color(byte r, byte g, byte b)
   {
     red = r;
     green = g;
     blue = b;
   }
}

In layman 1, an const assignment must be a value that can be initialized at runtime (const page p=null correct,const page p= new Page() incorrect, because new Page() needs to be initialized at runtime). static readonly(static readonly page p= new Page())

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: