Statements

A break statements terminate the nearest wile, do while, for or switch statement. A break affect only the nearest enclosing loop or switch.

As any block, variables declared inside a try block are inaccessible outsite the block - in particular, they are not accessible to the catch clauses.

Exception.

A try block maigh call a function that contains a try which calls another funciton with its own try and so on. The search for a hander reverses the call chain. If no appropriate catch is found, execution is transfered to a library function name terminate. The terminate will garantee to stop further execution of th problem.

If there are no try blocks, they can be no handlers. If a program no try blocks and an exception occurs, then terminate is called and program is exited.

Programs that properly "clean up" during execution handling are saied to be "exception safe".

Functions

Parameters and Arguments

Arguments are the initilizers for a function's parameters.

Local Objects

In C++, names have scopes, and objects have lifetimes

  • The scope of a name is the part of the program's text in which that name is visible.
  • The lifetime of an object is the time during the program's execution that the object exists.

Parameters and variables defined inside a function body are refered to as local variables. They are "local" to that function and hide declaration of the same name made on outer scope.

Obejcts defined outside any functions exist through the program's execution. Such object are created then the program starts and not destroyed until the program ends

The lifetime of varibles depends on how it is defined.

Automatic object. Objects that exist only when a block is execution are known as automatic objects.

local static object. Each local static object is initilized before the first time execution pass through the object's definition. Local static objects are not destroyed when a funciton ends; they are destroyed when the program terminated.

size_t count_calls(){
static size_t ctr = ; // value will persist across calss
ctr++;
return ctr;
}
int main(){
for (size_t i = ; i != ; i++){
cout << count_calls() << endl;
}
}

This program will print the number from 1 through 10 inclusive.

Function declartions are also known as the function prototype.

Recall that variables are declared in haeder files and defined in source file. Function should be declared in header files and defined in source file.

The header that declares a funciton should be included in that source file tha defines that function.

Argument Passing

Parameter initialization works the same way as variable initialization.

If the parameter is a reference then the parameter is bound to its argument. Otherwise, the argument's value is copied.

When aprameter is a reference, we say that its correspondency argument is "passed by reference", or that function is "called by referenced". As with any other reference, a reference paramter is an alisa for the object to which it is bound.

When the argument value is copied. the parametr and argument are independent object. We say such arguments are "passed by value", or the function is "called by value".

When we initiliaze a non-reference type variable or parameter, the value of the initializer is copied.

Pointers behave like any other non-reference type. When we copy a pointer, the value of the pointer is copied. After the copy, the two pointers are distinct, but pointing to the same object.

void reset(int *ip){
*ip = ; // change the value of the object to which ip points
ip = ; // change the local pointer; the argument is unchanged.
}

Usage:

int i = ;
reset(&i); // change i, but not the address of i

Programmers accustomed to programming in C often use pointer paramters to access objects outside a function. In C++, programmers generally use reference parameters instead.

Recall that operations on a reference are actully operations on the object to which the reference refers

void reset(int &i){    // i is just another name for the object passed to reset
i = ;       // change the value of the objet to which i refers
}

When we call this version of reset, pass an object directly, no need to pass its address.

int j = ;
reset(j); // passed by reference, the value on j is changed

Reference parameter that are not changed inside a function should be references to const.

Never return a reference or pointer to a local object.

When a function completes, its storage is free. After a function terminates, references to local object refer to memory that is no long valid.

// disaster: this function returns a reference to a local object
const string &mapip(){
string ret;
if(!ret.empty()){
return ret; // WRONG: return a reference to a local object
}else{
return "Empty"; // WRONG: "Empty" is a local object
}
}

References returns are lvalues

Calls to function that return references are lvalu. Other return types is rvalue.

char &get_val(string &str, string::size_type ix){
return str[it];
}
int main(){
string s("a value");
get_value(s, ) = 'A'; // change s[0] to A
return ;
}

Overload Functions

Functions that have the same name but different parameter lists and that appear in the same scope are overload.

Default Arguments

sz wd = ;
char def = ' ';
char ht(); string screen(sz = ht(), sz = wd, char = def); void f2(){
sz wd = ; // hide the definition of wd, but not change the default
def = '*'; // change the default
window = screen(); // call screen(ht(), 80, '*')
}

Reference:

C++ Primer, Fifth Edition, chapter 6 Functions

[C++] Fucntions的更多相关文章

  1. 【代码笔记】iOS-钢琴小游戏

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> //加入头文件 #import <AudioTool ...

  2. wordpress工作原理

    WP初始化的过程:当你输入<yourlink>/wordpress对wordpress进行初始化时,wordpress默认会找根目录下的index.php页面,看一下index.php页面 ...

  3. Objects and Data Structures

    Date Abstraction Hiding implementation is not just a matter of putting a layer of fucntions between ...

  4. 黄聪:wordpress工作原理

    WP初始化的过程:当你输入<yourlink>/wordpress对wordpress进行初始化时,wordpress默认会找根目录下的index.php页面,看一下index.php页面 ...

  5. C++11 之 &quot; = delete &quot;

    1  缺省函数 设计一个类,没有成员函数 (member function),只有成员数据 (member data) class DataOnly { private: std::string st ...

  6. C++11 之 " = delete "

    1  缺省函数 设计一个类,没有成员函数 (member function),只有成员数据 (member data) class DataOnly { private: std::string st ...

  7. Python 2.7的字典实现简化版(C语言)

    这是一个能自动调整大小的哈希字典,外部接口实现了下列功能. 1.字典级别: 创建字典 dict_new 归零字典 dict_clear 2.键值级别: 查找 dict_search 强制查找 dict ...

  8. R-CNN,SPP-NET, Fast-R-CNN,Faster-R-CNN, YOLO, SSD, R-FCN系列深度学习检测方法梳理

    1. R-CNN:Rich feature hierarchies for accurate object detection and semantic segmentation 技术路线:selec ...

  9. faster rcnn讲解很细

    https://blog.csdn.net/bailufeiyan/article/details/50749694 https://www.cnblogs.com/dudumiaomiao/p/65 ...

随机推荐

  1. hdu_5187_zhx's contest

    Problem Description As one of the most powerful brushes, zhx is required to give his juniors n probl ...

  2. JavaScript 中 this 的原理

    一.问题 学习 JavaScript 其中一个标志就是理解下面两种写法,会输出有不一样的结果. var obj = { foo: function () {} }; var foo = obj.foo ...

  3. JQuery制作网页——第九章 表单验证

    1.  表单验证:减轻服务器的压力.保证输入的数据符合要求: 2.  常用的表单验证:日期格式.表单元素是否为空.用户名和密码.E-mail地址.身份证号码等: 3.  表单验证的思路: 1.     ...

  4. ORALCE表的约束条件

    一.主键:(PRIMARY KEY) 主键是表中的一列或多列.为表定义主键有如下几个作用: 1.主键包含的列不能输入重复的值,以此来保证一个表的所有行的唯一性: 2.主键也不允许定义此约束的列为NUL ...

  5. phpstudy lamp

    phpStudy for Linux (lnmp+lamp一键安装包 现在不考虑安装这个  (完整版:http://lamp.phpstudy.net/phpstudy-all.bin) 安装: wg ...

  6. 如何用SQL语句处理缓慢变化维(渐变维,拉链表)SCD-2?

    假设有一张居民维表,需要记录居民状态的变更历史,根据Kimball建模理论,设计居民维表如下: 另外在ODS中有居民信息的每日快照表(每天都记录一份居民的全量信息):O_USERINFO 如何将ODS ...

  7. Apache Flume简介及安装部署

    概述 Flume 是 Cloudera 提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的软件. Flume 的核心是把数据从数据源(source)收集过来,再将收集到的数据送到指定的目 ...

  8. 对QP中RTC的理解

    1.概念 RTC(Run To Completion)是运行到完成为止的意思.在状态机中,从源状态到目标状态的转换动作要运行到完成. 从字面上来看,这个过程像是不可中断的,但实际并不是,这个过程可以被 ...

  9. 最小化的测试套件minimal_test的使用

    1:需要包含文件文#include <boost/test/minimal_test.hpp> 2:minimal_test内部实现了main(), 因此无需自己编写main()函数, 只 ...

  10. c语言程序设计:用strcpy比较数组(银行卡密码程序设计),strcpy(复制数组内容)和getchar()(敲键盘字符,统计不想要的字符的个数)

    统计从键盘输入一行字符的个数: 1 //用了getchar() 语句 2 //这里的\n表示回车 #include <stdio.h> #include <stdlib.h> ...