Chapter 7 Functions
7.2 -- Argument Passing
Note:
When the only reason to make a parameter a reference is to avoid copying the argument, the parameter should beconst reference.
It should be obvious that a function that takes a plain, nonconst reference may not be called on behalf of aconst object. After all, the function might change the object it is passed and thus violate theconstness of the argument.
Best Practices:
Reference parameters that are not changed should be references toconst. Plain, nonconst reference parameters are less flexible. Such parameters may not be initialized byconst objects, or by arguments that are literals or expressions that yield rvalues.
7.4 -- Function Declarations
Just as variables must be declared before they are used, a function must be declared before it is called. As with a variable definition, we can declare a function separately from its definition; a function may be defined only once but may be declared multiple times.
A function declaration consists of a return type, the function name, and parameter list. The parameter list must contain the types of the parameters but need not name them. These three elements are referred to as the function prototype. A function prototype describes the interface of the function.
Parameter names in a function declaration are ignored. If a name is given in a declaration, it should serve as a documentation aid:
void print(int *array, int size);
Recall that variables are declared in header files and defined in source files. For the same reasons, functions should be declared in header files and defined in source files.
Best Practices: The source file thatdefines the function should include the header thatdeclares the function.
A default argument is specified by providing an explicit initializer for the parameter in the parameter list. We may define defaults for one or more parameters. However, if a parameter has a default argument, all the parameters that follow it must also have default arguments.
A function that provides a default argument for a parameter can be invoked with or without an argument for this parameter. If an argument is provided, it overrides the default argument value; otherwise, the default argument is used.
7.7 -- Class Member Functions
A member function that is defined inside the class is implicitly treated as an inline function.
Let's look in more detail at the definition of same_isbn:
bool same_isbn(const Sales_item &rhs) const
{ return isbn == rhs.isbn; }
We now can understand the role of the const that follows the parameter lists in the declarations of theSales_item member functions: Thatconst modifies the type of the implicitthis parameter. When we calltotal.same_isbn(trans), the implicitthis parameter will be a const Sales_Item* that points tototal. It is as if the body ofsame_isbn were written as
// pseudo-code illustration of how the implicit this pointer is used
// This code is illegal: We may not explicitly define the this pointer ourselves
// Note that this is a pointer to const because same_isbn is a const member
bool Sales_item::same_isbn(const Sales_item *const this,
const Sales_item &rhs) const
{ return (this->isbn == rhs.isbn); }
A function that uses const in this way is called a const member function. Becausethis is a pointer toconst, aconst member function cannot change the object on whose behalf the function is called. Thus,avg_price andsame_isbn may read but not write to the data members of the objects on which they are called.
7.8 -- Overloaded Functions
Two functions that appear in the same scope are overloaded if they have the same name but have different parameter lists.
Functions cannot be overloaded based only on differences in the return type.
Two parameter lists can be identical, even if they don't look the same:
// each pair declares the same function
Record lookup(const Account &acct);
Record lookup(const Account&); // parameter names are ignored
typedef Phone Telno;
Record lookup(const Phone&);
Record lookup(const Telno&); // Telno and Phone are the same type
Record lookup(const Phone&, const Name&);
// default argument doesn't change the number of parameters
Record lookup(const Phone&, const Name& = "");
// const is irrelevent for nonreference parameters
Record lookup(Phone);
Record lookup(const Phone); // redeclaration
It is worth noting that we cannot overload based on whether the pointer itself isconst:
f(int *);
f(int *const);//redeclaration
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
콜백 함수를 Angular 하위 구성 요소에 전달이 예제는 구성 요소에 함수를 전달하는 것과 관련하여 최근에 직면한 문제를 다룰 것입니다. 국가 목록을 제공하는 콤보 상자 또는 테이블 구성 요소. 지금까지 모든 것이 구성 요소 자체에 캡슐화되었으며 백엔드에 대한 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.