Main expression
A primary expression is a building block of more complex expressions. They are text, name, and range resolution operators (::) The qualified name. A primary expression can have any of the following forms:
Literal
This
:: Name
name
(expression)
Literal is a constant main expression. Its type depends on the form of its specification.
The This keyword is a pointer to a class object. It is available in non-static member functions and points to an instance of the class for which the function is called. The This keyword can only be used outside of a class member function body.
This pointer is of type *const in a function that does not specifically modify the this pointer (where type is the class name). The following example shows the member function declaration and the type of this:
Expre_primary_expressions.cpp
//compile with:/ld
class Example
{public
:
void Func (); * const this
void Func () const; CONST * const this
void Func () volatile;//volatile * const this
};
Range resolution operator (::) followed by the name constitutes the primary expression. This class name must be a globally scoped name, not a member name. The type of this expression is determined by the declaration of the name. If the declared name is a left value, the type is a left value (that is, it can appear to the left of an assignment operator expression). The scope resolution operator allows reference to global names, even if the name is hidden in the current scope.
An expression enclosed in parentheses is a primary expression that has the same type and value as an expression without parentheses. If an expression with no parentheses is a left value, the expression enclosed in parentheses is also the left value.
In the context of the primary expression syntax given above, name is represented as anything in the syntax of the name description, but the type of name that can only appear in the class is not allowed when using the range resolution operator before the name. This includes user-defined conversion function names and destructor names.
Examples of primary expressions include:
M//literal
' C '//literal this
//in a member function, a pointer to the class instance
:: Func//A global function
:: operator +//A global operator function
:: A::B//A Global qualified name
(i + 1)//a parenthe Sized expression
The following example is all of the considered name and various forms of the main expression:
MyClass//A identifier
MYCLASS::F//A qualified name
operator =//an operator function name
operator Char *//A conversion operator function name
~myclass//A destructor name
a::b //A qualified name
A<i nt>//a template ID
Suffix expression
A suffix expression contains a primary expression, or an expression in which the suffix operator follows the primary expression. The following table lists the suffix operators.
Suffix operator
Operator name
Operator notation
Subscript operator
[ ]
Function call operator
( )
Explicit type-conversion operators
Type-name ()
Member access operators
. or –>
Suffix increment operator
++
Suffix decrement operator
––
The following syntax describes a possible suffix expression:
Primary-expression
postfix-expression [expression]
postfix-expression (expression-list)
Simple-type-name (expression-list)
postfix-expression. Name
postfix-expression–> name
Postfix-expression + +
postfix-expression––
Cast-keyword < typename > (expression)
typeid ( TypeName)
The postfix-expression above may be either a primary expression or another suffix expression. See also primary expression. Suffix expressions are grouped from left to right, allowing expressions to be linked as follows:
func (1)->getvalue () + +
In the expression above, Func is the primary expression, func (1) is a function suffix expression, func (1)-> GetData is a suffix expression that specifies a class member, Func (1)->getdata () is another function suffix expression, and the entire expression is a suffix expression that increases the return value of the GetData. The overall meaning of the expression is to pass the call Func of 1 as a parameter and get a pointer to the class as the return value. It then calls the GetValue () on this class, and then increments the returned value.
The expression listed above is an assignment expression, which means that the result of these expressions must be a right value. The
suffix expression form
simple-type-name (expression-list)
indicates a call to a constructor. If Simple-type-name is a base type, the expression list must be a single expression, and the expression indicates that the value of the expression will be converted to the underlying type. This class casts an expression to mimic a constructor. Because this form allows the use of the same syntax to construct basic types and classes, it is particularly useful when defining template classes. The
Cast-keyword is one of dynamic_cast, static_cast, or reinterpret_cast. More information can be found in dynamic_cast, static_cast, and Reinterpet_cast. The
typeID operator is treated as a suffix expression. See typeid operator. The
formal parameters and arguments
Caller passes the information to the called function in the argument. Called functions use the corresponding "formal parameters" to access information.
When a function is called, the following tasks are performed:
evaluates all arguments (the caller-supplied arguments). The implied order of these parameters is not computed, but all parameters are evaluated, and all side effects are completed before entering the function. The
initializes the parameter using the corresponding argument in the expression list for each parameter. (a parameter is an argument declared in the header of a function and used in the body of a function.) The conversion is done as if it had been initialized-a standard and user-defined transformation is performed when the argument is converted to the correct type. The following code demonstrates the initialization performed conceptually:
void Func (int i); Function prototype ...
Func (7); Execute function call
The conceptual initialization before the call is:
int temp_i = 7;
Func (temp_i);
Note that initialization is performed as if you were using the equal sign syntax rather than the parentheses syntax. A copy of I was made before the value was passed to the function.
Therefore, if the function prototype (declaration) invokes a long parameter, and the calling program provides an argument of type int, the argument is promoted using a standard type conversion to a long type.
If an argument is provided but it does not have a standard or user-defined conversion to the type of the parameter, it is an error.
For arguments of a class type, the Huafing parameter is initialized by calling the class's constructor.
Performs a function call.
The following fragment shows a function call:
Expre_formal_and_actual_arguments.cpp
void func (Long param1, double param2);
int main ()
{
long i = 1;
Double j = 2;
Call Func with actual arguments I and J.
Func (I, j);
}
Define func with formal parameters param1 and param2.
void func (Long param1, double param2)
{
}
When you call Func from main, the initial Huafing parameters are used param1 (I will convert to type Ilong to correspond to the correct type using the standard conversion), and the initial param2 parameter is used jdouble (J converts to the type Huafing using standard conversions).
Handling of parameter types
You cannot change a formal parameter declared as a const type within a function topic. A function can change any argument that a type is not a const. However, the change is local to the function and does not affect the value of the argument unless the argument is a reference to an object that is not a const type.
The following functions illustrate some of these concepts:
Expre_treatment_of_argument_types.cpp
int func1 (const int i, Int J, char *c) {
i = 7; C3892 i is const.
j = i; The value of J is lost in return
*c = ' a ' + J; Changes value of C in calling function return
i;
}
double& Func2 (double& D, const char *c) {
d = 14.387; Changes value of D in calling function.
*c = ' a '; C3892 c is a pointer to a const object.
return D;
}
Ellipses and Default parameters
By using one of the following two methods, you can declare a function to accept parameters that are less than the parameters specified in the function definition: ellipses (...) or default parameters.
An ellipsis indicates that a parameter may be required, but the number and type are not specified in the declaration. This is usually a poor C + + programming practice because it gives you no access to the benefits of C + +, which is type safety. Different transformations will be applied to functions that use ellipsis declarations, not to those that are known to have their formal parameters and argument types:
If the type of the argument is floating-point, it is promoted to a double-precision type before the function is called.
Converts any signed or unsigned char, short, enumerated type, or bit field to a signed or unsigned int using an integral elevation.
All parameters of a class type are passed as data structures by value, and replicas are created by binary replication, not by calling the class's copy constructor, if one exists.
If you use an ellipsis, you must declare it at the end of the argument list.
If a value is not supplied in a function call, you can specify the value that the parameter should take by using the default parameter. The following code fragment shows how the default parameters work.
Expre_ellipses_and_default_arguments.cpp
//compile with:/EHsc
#include <iostream>//
Declare The function print that prints a string,
//Then a terminator.
void print (const char *string,
const char *terminator = "\ n");
int main ()
{
print ("Hello,");
Print ("world!");
Print ("Good Morning", ",");
Print ("Sunshine.");
using namespace std;
Define Print.
void print (const char *string, const char *terminator)
{
if (string!= NULL)
cout << string;
if (Terminator!= NULL)
cout << Terminator;
}
The above program declares a function print with two arguments. The second parameter, Terminator, has the default value "\ n". In Main, the first two calls to print allow the default second parameter to provide a new line to terminate the printed string. The third call specifies an explicit value for the second parameter. The output of the program is
Hello,
world!
. Good morning, sunshine.