Directory
Basic principle
Discard Managed Extensions
The fun of the future
The evolution of programming
Ultimate goal
Concluding remarks, though not goodbye
This month, I'm going to change the regular question and answer form to tell you about a very good document I found on the Internet. A few weeks ago, someone wrote to ask me why he couldn't declare the const function in C++/CLI:
// 引用类
ref class A {
void f() const; // 不!
};
To this, I replied: You just can't, this is the rule. The common Language Infrastructure (CLI) is designed to support, for example, Visual basic®, Java, and even COBOL languages-these languages do not even know the meaning of const. Because the CLI does not know what the const member function is, you cannot use it.
After I finished my reply, I vaguely remembered something buried in my memory, about const, about how to handle compiler hints so that other languages can ignore these prompts. I looked up the previous column and found that I answered a question about const in July 2004. In fact, C++/CLI does allow you to declare const data members and arguments-but not const member functions. Figure 1 shows a small program that has a reference class with a const static data member. If you compile this program and then use ILDASM to disassemble it, you will see information similar to the following:
field public static int32
modopt([mscorlib]System.Runtime.CompilerServices.IsConst)
g_private = int32(0x00000001)
Figure1const.cpp
////////////////////////////////////////////////////////////////
//To compile Type:
//CL/CLR const.cpp
#include <stdio.h>
ref class A {
int m_val;
Allows constant data members to be generated modopt
static const int g_private = 1;
Public:
//Common constant members can be used by Visual Basic or other programs that do not recognize constants
To modify (and thus use the literal volume).
Literal int g_public = 1;
A (int i) {m_val = i;}
void print ();//const; No! CONSTANT FN
} is not allowed;
void A::p rint ()
{
printf ("Val is%d\n", m_val);
}
Int main ()
{
A a);
A.print ();
}
Modopt (optional modifier) is an MSIL declaration for the CLI consumer: If you understand it, it's good; if you don't understand it, you can ignore it completely. Instead, modreq (the desired modifier) means that you cannot use this function if you do not understand it. Volatile is an example of modreq. Because a volatile reference can be changed at any time by the operating system/hardware (or even another thread), it is better for the CLI user to know what volatile means if they want to use the volatile object. But the const is optional. Note that although Managed Extensions converts the C + + const object into CLI literal volume, C++/CLI does not do so. So be careful-if you declare a public const data member, a client written in a language like visual Basic can change its value. If you want the CLI client to be unable to change the value, you should declare the object literal, as shown in Figure 1. But what about the member function-why is the const member function not allowed?