1. Apart from static data members, data members cannot be explicitly initialized in the class body.
For example:
Class first {
Int Memi = 0; // Error
Double memd = 0.0; // Error
};
Class data members are initialized through the class constructor.
2. We can declare a class but not define it.
For example:
Class Screen; // screen class declaration
This statementProgramA screen name is introduced to indicate that screen is a class type.
However, we can only use declared but not defined class types in a limited way. If no class is defined, we cannot define objects of this type. Because the size of the class type is unknown, the compiler does not know how much storage space is reserved for the class type objects.
However, we can declare a pointer or reference to the class type, and allow the pointer and reference because they both have a fixed size, which has nothing to do with the size of the object they direct to. However, because the size and members of the class are unknown, we have to wait until the class is fully defined before applying the unreferenced operator (*) to such a pointer, you can also use pointers or references to point to a class member.
Only when the definition of a class has been seen can we declare a data member as an object of this class. The definition of this class is not displayed in the program text. The data member can only be a pointer or reference of this type. For example, the following is the definition of stackscreen class. It has a data member pointing to the screen class pointer. Here, only the declaration of screen is not defined:
Class Screen; // Declaration
Class stackscreen {
Int topstack;
// OK: Specifies a screen object.
Screen * stack;
Void (* Handler )();
};
Because a class is considered to have been defined only when its class body is complete, a class cannot have its own data members. However, when the Class header of a class is viewed, it is regarded as declared. Therefore, a class can use a pointer to its own type or reference as a data member. For example:
Class linkscreen {
Linkscreen ilinkscreen; // error: A class cannot have its own data members.
Linkscreen * next;
Linkscreen * Prev;
};
3. inline and non-inline member functions
The member function defined in the class body is an inline function by default, which can be displayed with Inline;
Generally, a member function defined in the class in vitro is not inline. However, such a function can also be declared as an inline function. You can use the keyword inline explicitly in the function declaration that appears in the class body, or explicitly use the keyword inline through the function definition that appears in the class body in vitro, or both.
4. Const member functions
In general, any action in the program to modify the const object will be marked as a compilation error.
Const char blank = '';
Blank = '\ n'; // Error
However, a program usually does not directly modify class objects, but calls the public member function set only when class objects must be modified. In order to respect the constants of class objects, the compiler must distinguish between insecure and secure member functions (that is, to distinguish between functions that attempt to modify class objects and do not attempt to modify class objects). For example:
Const screen blankscreen;
Blankscreen. Display (); // read Class Object
Blankscreen. Set ('*'); // error: Modify Class Object
The class designer declares member functions as const to indicate that they do not modify class objects. For example:
Class Screen {
Public:
Char get () const {return _ screen [_ cursor];}
//...
}
Only a member function declared as const can be called by a const class object. The keyword const is placed between the parameter table of the member function and the function body. For a const member function defined outside the class body, we must specify the keyword const in its definition and description.
It is illegal to declare a function that modifies a class data member as Const. For example, in the following simplified screen definition:
Class Screen {
Public:
Int OK () const {return _ cursor ;}
Void error (INT ival) const {_ cursor = ival ;}
//...
PRIVATE:
String: size_type _ cursor;
//...
};
The definition of OK () is a valid const member function definition, because it does not change the value of _ cursor, but the definition of error () modifies the value of _ cursor, therefore, it cannot be declared as a const member function. This function definition will cause the following compiler error message:
Error: cannot modify a data member within a const member function
However, declaring a member function as const does not prevent all possible modifications by the programmer. Declaring a member function as const ensures that the member function does not modify the data members of the class. However, if the class contains pointers, you can modify the objects indicated by the pointer in the const member function. The compiler will not detect such changes as errors. For example:
# Include <cstring>
Class text {
Public:
Void bad (const string & parm) const;
PRIVATE:
Char * _ text;
};
Void text: Bad (const string & parm) const
{
_ Text = parm. c_str (); // error: _ text cannot be modified.
For (int ix = 0; ix <parm. Size (); ++ IX)
_ Text [ix] = parm [ix]; // bad style, but not wrong
}
Although _ text cannot be modified, _ text is of the char * type. In the const member function of the text class, you can modify the characters pointed to by _ text. The member function is bad () reflects a poor programming style.
Const member functions can be overloaded by non-const member functions of the same parameter table. For example:
Class Screen {
Public:
Char get (int x, int y );
Char get (int x, int y) const;
//...
};
In this case, the constant nature of the Class Object determines which function to call:
Int main (){
Const screen Cs;
Screen S;
Char CH = cs. Get (); // call the const Member
Ch = S. Get (); // call a non-const Member
}
Const and destructor are two exceptions. Even if they are not const member functions, const class objects can call them. When the execution of the constructor is complete and the class object has been initialized, the constant of the Class Object is established. When the Destructor is called, the constant will disappear. Therefore, a const class object is considered as const during this time period from the time when the const class object is constructed to the time when the Destructor starts.