Home>

C++ class member variables and member functions

A class is a data type,It's similar to ordinary data types,But it is different from ordinary data types.The class data type is a collection of member variables and member functions.

Class member variables are the same as ordinary variables.There are also data types and names,Occupies a fixed length of memory space.However, you cannot assign values ​​to member variables when defining classes.Because a class is just a data type,Does not take up memory space itself,The value of a variable requires memory to store it.

The member functions of the class are the same as ordinary functions.Has a return value and a parameter list,It differs from general functions in that member functions are members of a class.Appears in the class body,Its scope is determined by the class;While ordinary functions are independent,The scope is global,Or in a namespace.

In the last section we gave the definition of the student class in the last complete example,As follows:

class student {
public://Variables included in the class
  char * name;
  int age;
  float score;
public://class contains functions
  void say () {
    printf ("The age of%s is%d, the grade is%f \ n", name, age, score);
  }
};
The above code defines member functions in the class body.
You can also declare functions only in the class body,And put the function definition outside the class body,As shown below:
class student {
public:
  char * name;
  int age;
  float score;
public:
  void say ();//function declaration
};
//function definition
void student ::say () {
  printf ("The age of%s is%d, the grade is%f \ n", name, age, score);
}

When defining a function directly in the class body,No need to prefix the function name with the class name,Because the class to which the function belongs is self-evident.

But when member functions are defined outside the class,You must qualify the function name with a class name.::is called a domain resolver (also called a scope operator or scope qualifier), and is used to connect class names and function names,Indicates which class the current function belongs to.

If there is no class name before the domain resolver "::",Or there is neither a class name nor a domain parser "::" before the function name, such as:

//no class name
::say () {
  //todo
}
//No class name or domain parser
say () {
  //todo
}

It means that the say () function does not belong to any class,This function is not a member function,But global functions,That is, the ordinary functions of non-member functions.

Member functions must be prototype declared in the class body.And then define it outside the class,In other words, the position of the class body should be before the function definition.Otherwise, errors will occur during compilation.

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

inline member functions

There is a difference between defining a member function in a class body and outside a class:Member functions defined in a class body are inline functions, but not defined outside a class.

Inline functions are generally not what we expect,It will replace the function call with the function body,So I recommend making declarations for member functions inside the class body,And defined outside the class body,This is a good programming practice.

Of course, if your function is short,Want to be defined as an inline function,There is nothing wrong with that.

If i want to define the function outside the class body,I hope it is an inline function,Then you can add the inline keyword when declaring the function, as follows:

class student {
public:
  char * name;
  int age;
  float score;
public:
  inline void say ();//Declared as an inline function
};
//function definition
void student ::say () {
  printf ("The age of%s is%d, the grade is%f \ n", name, age, score);
}

That way, say () becomes an inline function.

Functions defined inside the class body can also have the inline keyword, but this is redundant,Because the functions defined inside the class body are inline functions by default.

It is worth noting thatIf you define an inline function outside the class, you must put both the class definition and the member function definitions in the same header file (or in the same source file), otherwise the embedding cannot be performed at compile time (embedding the function code into the function Call out). Although this improves the execution efficiency of the program,But from a software engineering quality perspective,This is not a good idea,As a result, inline functions are rarely used in classes in actual development.

C++ proposes the main purpose of inline functions:to replace inline function definitions with parameters (functions are more convenient to use than reference macros), rather than to improve program operation efficiencyBecause compared to the time it takes to execute the function,The time it takes to call a function is often minimal.

C++ member function storage

When using classes to define objects,The system allocates storage space for each object.If a class includes data and functions,Allocate storage space for data and function code separately.

Theoretically, if 10 objects are defined by the same class, then storage units need to be allocated for the data and function codes of the 10 objects,As shown below.

Can we use only one space to store this common function code segment,When calling the function of each object,To call this common function code.as the picture shows.

Obviously, this will greatly save storage space.The c++ compilation system does exactly this,Therefore, the storage space occupied by each object is only the storage space occupied by the data portion of the object.It does not include the storage space occupied by the function code.If a class is declared:

class time
{
  public:
  int hour;
  int minute;
  int sec;
  void set ()
  {
   cin>>a>>b>>c;
  }
};

You can use the following statement to output the number of bytes occupied by this type of object:

 cout<sizeof (time)<endl;

The output value is 12.

This proves that the amount of space occupied by an object depends only on the space occupied by data members in the object,It has nothing to do with member functions.

Function code is stored outside the object space.If 10 objects are defined for the same class, the member functions of these objects correspond to the same function code segment,Instead of 10 different function code snippets.have to be aware of is,Although member functions of different objects are called to execute the same function code,But the execution results are generally different.

Different objects use the same function code snippet,How can it operate on data in different objects separately?

It turns out that C++ specifically set up a pointer named this to point to different objects.Need to explain:

Regardless of whether a member function is defined inside or outside the class,Member function code segments are stored in the same way.

Don't confuse this way of storing member functions with the concept of inmne (built-in) functions.Don't mistake the member functions declared inline (or default to inline),Its code snippet takes up storage space on the object,Without using the member function declared inline,Its code snippet does not take up storage space of the object.Regardless of whether it is declared inline or not, the code section of the member function does not occupy the storage space of the object.The effect of inline declaration is that when the function is called,Copy and insert the code snippet of the function to the function call point,Without inline declaration, when calling this function,The process transfers the population address of the function code segment,After executing the function code snippet,The process returns to the function call point.Inline is not related to whether the member function takes up storage space of the object,They are not the same problem,It should not be confused.

It should be stated,The "member function of an object" is often said from a logical point of view.And the way member functions are stored,From a physical point of view,The two are not contradictory.

c
  • Previous Detailed Merge usage in SQL
  • Next C # implements the ComboBox control to display multiple data source properties