Learn C ++ Primer (7) again-Function

Source: Internet
Author: User

It is not suitable for copying real parameters:
1) when you need to modify the value of the real parameter in the function;
2) When a large object needs to be used as a real parameter;
3) when there is no way to implement object replication;


Return additional information using reference parameters
# Include <iostream>
# Include <vector>
Using namespace std;
// Function Description: Find the number of occurrences of a specific value in the range, and return the iterator that appears for the first time.
Vector <int >:: const_iterator findVal (vector <int >:: const_iterator begin, vector <int >:: const_iterator end, int value, vector <int >:: size_type & occurs)
{
Vector <int>: const_iterator p = end;
Occurs = 0;
For (; begin! = End; ++ begin)
If (* begin = value)
{
If (p = end) p = begin;
++ Occurs;
}
Return p;
}
Int main ()
{
Vector <int> iVector;
Vector <int>: size_type;
IVector. push_back (1 );
IVector. push_back (2 );
IVector. push_back (3 );
IVector. push_back (2 );
Vector <int>: const_iterator iter = findVal (iVector. begin (), iVector. end (), 2, );
Cout <a <* iter <"Hello world! "<Endl;
Return 0;
}
 
If the only purpose of using a reference parameter is to avoid copying a real parameter, you should define the parameter as a const reference.


Reference parameters that do not need to be modified should be defined as const references. Common Non-const reference parameters are not flexible in use. Such parameters cannot be initialized by a const object, nor by a literal value or an expression that generates the right value.


Normally, functions should have vector or other standard library container-type parameters. Calling a function that contains common non-reference vector parameters will copy every element of the vector.


Defining an array parameter as a pointer is better than defining it using array syntax. Setting it as an array parameter may cause misunderstanding and cross-border errors.


You do not need to modify the element of the array parameter. The function should define the parameter as a pointer to the const object. For example, void f (const int *).


Do not return a reference to a local object.


Ask for an appointment (HP intern interview ):
# Include <iostream>
# Include <math. h>
Using namespace std;
Int rgcd1 (int a, int B); // recursive Solution
Int rgcd2 (int a, int B); // Iterative Solution
Int main ()
{
Cout <rgcd1 (9, 6) <endl;
Cout <rgcd2 (16, 12) <endl;
Cout <"Hello world! "<Endl;
Return 0;


}
Int rgcd1 (int a, int B)
{
If (B! = 0) return rgcd1 (B, a % B );
Else return;
}
Int rgcd2 (int a, int B)
{
Int t;
If (B = 0 | a = 0) return (abs (a-B ));
T = a % B;
While (t)
{
A = B; B = t; t = a % B;
}
Return B;


}


When defining a function, if a parameter has a default real parameter, all the following parameters must have a default real parameter.
When designing a function with default real parameters, you must arrange the parameters so that the least number of default real parameters is used.
The first parameter is the parameter that can be used by default.


Once a static local object is created, it is not revoked until the main program ends. It can be used as a counter. The life cycle is the entire source program, but the scope is the same as that of automatic variables.
Comply with general access rules.


About static data members: For all objects with one member variable, use static, providing a member variable shared by all objects is more effective than "maintaining a member variable for each class object.
Each class object has its own copy, and the static data member has only one copy static data member for each class type, and only one copy is shared by all the objects of this class type. Compared with global objects, static data members have two advantages:
1. The static data member does not enter the global namespace of the program, so there is no possibility of conflict with other global names in the program.
2. Information Hiding: static members can be private members, but global objects cannot.
 
About static function members:
Static member functions: function members declared using the static keyword make static member functions belong to the entire class, and are maintained by all objects of the same class and shared by these objects.

Static member functions can only access static member variables. to access non-static member variables, you can only access non-static member variables and static member functions of an object. You can pass a pointer to an object and reference other parameters to this static member function.

 


When the class definition body references the static member of the class, you must specify the class defined in the member. The static keyword can only be used in the declaration of the class definition body. The definition cannot be marked as static.

 
Inline)
Benefits of defining a small operation as a function:
1) good readability 2) high maintainability 3) reusable
Defining an inline function can avoid function call overhead.
Inline functions should be defined in the header file.


The member functions of a class can be defined either within the class or outside the class,
The compiler implicitly treats the member functions defined in the class as inline functions.


The const member function must be added to the const limitation in both the function prototype signature and function definition to indicate that it is a member function that does not change the object.


A const object, pointer, or reference can only be used to call its const member function. A non-const member function may attempt to modify the data member of a constant.


Overload function: two functions in the same scope have the same name but different form parameters. They cannot be implemented only based on different return types.
This parameter can be affected only when it is a reference or pointer.


The pointer to the function is the function entry. The example is as follows:
# Include <iostream>


Using namespace std;
Int max (int a, int B );


Int main ()
{
Int c;
Int (* p) (int, int );
P = max;
C = (* p) (9, 6 );
Cout <c <"Hello world! "<Endl;
Return 0;


}
Int max (int a, int B)
{
Return a> = B? A: B;
}


Typedef can be used for more intuitive definition,
For example, typedef char (* PTRFUN) (int );
PTRRFUN pFun;
When typedef is used, a new type is defined. The first sentence is to define a PTRFUN type and define this type as a pointer to a function. This function uses an int as a parameter, and returns the char type. The second row uses this new type to define the pointer function.
 

From left-brain design, right-brain Programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.