Parameter passing and return value

    In C++, if a member function of a class is declared, the modifier const is added after the parameter column (parentheses) and before the function body (curly braces), then the member function is called a constant member function of the class . Let's look at the following sample program:

class complex
{
public:
  complex (double r = 0, double i = 0)
    : re (r), im (i)
  { }
  complex& operator += (const complex&);
  double real ( ) const {return return;} //constant member function
  double imag ( ) const {return im;} //constant member function
private:
  double re, im;

  friend complex& __doapl (complex*, const complex&);
};

    Functions in C++ are divided into those that change data and those that do not. For functions that do not change data, const must be added. Const can modify both functions and objects.

    For constant member functions, there are the following properties:

    (1) A constant object can only call its constant member functions, not ordinary member functions;

    (2) Ordinary objects can call both constant member functions and common member functions;

    (3) Common member functions can access constant member functions of this class;

    (4) Constant member functions cannot access ordinary member functions of this class.

    If present in the main function:

{
  complex c1(2,1); //Ordinary object
  cout << c1.real();
  cout << c1.imag();
}

is legal, that is, the ordinary object described by property 1 calls its constant member function.

    If member functions and objects are:

class complex
{
  ...
  double real ( ) {return re;} //Ordinary member function
  double imag ( ) {return im;}
  ...
}

{
  const complex c1(2,1); //constant object
  cout << c1.real( ); //invalid
  cout << c1.imag( );
}

This kind of call is illegal, because constant objects cannot call ordinary member functions, because the compiler will think that the function may change the value of the constant object.

    Parameter passing: pass by value vs pass by reference (to const)

    There are two ways to pass parameters in C++: pass by value and pass by reference. The essence of pass-by-value is that the formal parameter is a copy of the actual parameter, and the change of the formal parameter does not change the value of the actual parameter. When passing by reference, the formal parameter and the actual parameter are actually the same thing, and changing the value of the formal parameter will also change the value of the actual parameter. The underlying essence of the reference is a pointer constant, which points to the referenced object. Pass-by-reference is recommended in C++.

    Return value passing: return by value vs return by reference (to const)

In the following example:

class complex
{
public:
  complex (double r = 0, double i = 0) //pass by value
    : re (r), im (i)
  { }
  complex& operator += (const complex&); //return by reference && pass by reference
  double real ( ) const {return re;}
  double imag ( ) const {return im;}
private:
  double re, im;

  friend complex& __doapl (complex*, const complex&);
};
{
  complex c1(2,1);
  complex c2;
 
  c2 += c1;
  cout << c2;
}

    Similarities and differences between "reference" and "pointer":

    Same point:

    (1) It is the concept of address;

    The pointer points to a piece of memory, and its content is the address of the memory pointed to; the reference itself is an alias for the target variable, and the operation on the reference is actually the operation on the target variable.

    difference:

    (1) A pointer is an entity, and a reference is just an alias;

    (2) Dereference (*) is not required for reference use, while pointers do;

    (3) A reference can only be initialized once at the time of definition, and is immutable after that, while a pointer is mutable;

    (4) References have no const, and pointers have const;

    (5) The reference cannot be null, and the pointer can be null;

    (6) "sizeof reference" gets the size of the pointed variable (object), and "sizeof pointer" gets the size of the pointer itself (the address of the pointed variable or object);

    (7) The self-increment (++) operations of pointers and references have different meanings;

    (8) From the point of view of memory allocation: the program allocates the memory area for the pointer variable, and the reference does not need to allocate the memory area.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325891860&siteId=291194637