member functions for C + + classes (define member functions outside the class, inline member functions)

Source: Internet
Author: User

The member function of a class is one of the functions, its usage and function is basically the same as the functions described earlier, it also has the return value and the function type, it differs from the general function only: it belongs to a member of a class, appears in the class body. It can be designated private (private), public (common), or protected (protected).

When using a class function, be aware of the permission that invokes it (it can be called) and its scope (the data and functions in what range the function can use). For example, a private member function can only be called by other member functions in this class, not by outside the class. member functions can access any member of the class (both private and public) and can reference data that is valid in this scope.

It is common practice to designate member functions that need to be called by the outside world as public, which are the external interfaces of the class. It should be noted, however, that it is not required to designate all member functions as public. Some functions are not intended for external invocation, but are called for member functions in this class and should be designated as private. This function is to support the operation of other functions, which are the tool functions of other members of the Class (utility function), which cannot be invoked by users outside the class.

The member function of a class is a very important part of the class body. If a class does not contain a member function, it is equivalent to the structure in C language, and does not show the function of class in object-oriented programming.

Defining member functions outside of a class

The member functions have been previously seen defined in the class body. You can also write only the declaration of a member function in the body of a class, and the function definition outside of the class. Such as:

Class student{public:void display ();//Public member function prototype declaration private:int num;string Name;char sex;//above 3 rows are private data members};void Student:: Display ()//define display class functions outside the class {cout<< "num:" <<num<<endl;cout<< "Name:" <<name<< endl;cout<< "Sex:" <<SEX<<ENDL;} Student stud1,stud2; Defining two classes of objects

Note: when defining a function directly in the body of a class, you do not need to precede the function name with the class name, because which class the function belongs to is self-evident .

However, when the member function is defined outside the class, it must precede the function name with the class name, which is qualified (qualifed), "::" is the scope qualifier (field qualifier) or the scope operator, which declares which class the function belongs to.

If there is no class name before the scope operator "::", or the function name has no class name and no scope operator "::", as
::d isplay () or display ()
The display function does not belong to any class, this function is not a member function, but a global function, that is, a general normal function of a non-member function.

A class function must first be declared in the class body and then defined outside the class, meaning that the class body should be positioned before the function definition, or it will compile with an error .

Although the function is defined outside the class, the function is executed when the member function is called and the function's definition (function code) is found based on the function prototype declared in the class.

It is a good habit of programming to declare member functions inside a class, and to define member functions outside the class body. If a function has only 2-3 rows of its function body, it can generally be defined in the class body when declaring the class. Functions that are more than 3 rows are generally declared within the class and are defined outside the class.

Inline member functions

For the built-in (inline) functions, which have been described in the front, please bash: C + + built-in functions. A member function of a class can also be specified as a built-in function.

member functions defined in the class body are generally small in size, and the process of calling a function is relatively expensive. The time spent calling a function is much larger than the execution time of all statements in the small-scale function body. To reduce the time overhead, if control structures such as loops are not included in the member functions defined in the class body, the C + + system automatically handles them as built-in (inline) functions .

That is, when a program calls these member functions, it does not actually execute the function's calling procedure (such as preserving the return address, etc.), but instead embeds the function code in the program's call point. This can greatly reduce the time overhead of calling member functions. C + + requires that the generic built-in function be declared with the keyword inline, but for member functions defined within the class, you can omit inline because these member functions have been implicitly specified as built-in functions. Such as:

Class student{public:void display () {cout<< "num:" <<num<<endl;cout<< "Name:" <<name <<endl;cout<< "Sex:" <<SEX<<ENDL;} Private:int num;string name;char sex;};

Where line 3rd
void display ()
can also be written
inline void display ()
Explicitly declare the display function as a built-in function.

The above two formulations are equivalent. For functions defined within a class, it is generally written inline.

It should be noted that if the member function is not defined in the class and is defined outside the class body, the system does not default to the built-in (inline) function, and the procedure to invoke these member functions is the same as the procedure for invoking the general function. If you want to designate these member functions as built-in functions, you should explicitly declare them with inline . Such as:

Class Student{public:inline void display ()//declares this member function as a built-in function private:int num;string name;char sex;}; inline void Student::d isplay ()//define the display function outside the class as a built-in function {cout<< "num:" <<num<<endl;cout<< "Name: "<<name<<endl;cout<<" Sex: "<<SEX<<ENDL;

As mentioned earlier, the declaration of a function or the definition of a function is an inline declaration . It is important to note that if you define an inline function outside the class body, you must place the definition of the class definition and the member function in the same header file (or in the same source file), or you will not be able to displace it at compile time (the copy of the function code is embedded in the function call point). However, it is not conducive to the separation of the class interface and the implementation of the class, which is not conducive to information concealment. Although the execution of the program is more efficient, it is not a good idea from the point of view of software engineering quality. This member function is specified as a built-in function only if the member function defined outside the class is small and the frequency of the call is high.

member functions for C + + classes (define member functions outside the class, inline member functions)

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: 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.