Chapter 7 Functions

本文详细阐述了C++中参数传递的最佳实践,包括使用const引用以避免复制对象,以及函数声明的规范。此外,文章还讨论了默认参数、类成员函数的特性,以及重载函数的概念。通过深入解析这些关键概念,读者将获得优化C++代码结构的实用技巧。

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




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值