Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > c++ primer(第四版)学习札记 10-8
【标  题】:c++ primer(第四版)学习札记 10-8
【关键字】:c++,primer,10-8
【来  源】:BLOG.CSDN.NET

c++ primer(第四版)学习札记 10-8

Your Ad Here
1、来看这段程序:
     char &get_val(string &str, string::size_type ix)
     {
         return str[ix];
     }
     int main()
     {
         string s("a value");
         cout << s << endl;   // prints a value
         get_val(s, 0) = 'A'; // changes s[0] to A
 
         cout << s << endl;   // prints A value
         return 0;
     }
会发现一个奇怪的用法,就是对函数的返回进行赋值,这就是返回引用的特点,引用返回都是Lvalue的,而get_val(s, 0)就是返回的元素的别名。
 
2、同时不要忘记,不要返回一个指向函数内部变量的指针,会造成错误。
 
3、根据默认参数的使用约定,最少使用的默认参数应放在前头,而使用最多的默认参数要放在后头(默认参数总是从要替换的那个参数开始到结束)
 
4、static的函数内部对象,是否只在第一次被初始化呢?根据示例程序来看,答案是是。
 
5、和其他函数不同,inline函数必须在头文件中定义。
 
6、在类的成员函数中,如果是在类中定义的,会被隐式的当成inline函数。
 
7、const member funtion:在函数的隐式参数中,this是指向const的指针,所以,const member funtion是不能改变类中的成员的,只读。
 
8、
//default constructor needed to
//initialize members of built-in type
Sales_item(): units_sold(0), revenue(0.0) { }
与java不同的是,在构造函数中可以以以上形式初始化成员数据。在冒号与左{之间的东东称为Constructor Initialization List。由编译器生成的默认构造函数,可以初始化一些class类型的成员数据,但是,若要初始化内置类型,则要用以上的方式显示初始化。
 
9、以下参数表的不同,并不能让编译器区别而进行函数重载:
(1)参数表中省略名字和未省略名字
(2)使用了typedef的
(3)使用了默认参数的
(4)非引用使用const限定和不使用的
(5)返回值的不同
例:
     // 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
编译器只在乎参数的类型和个数

10、当函数重载出现编译器不知道选择那个好时,可以使用强制类型转换来显示决定编译器的选择,但是在实践中,这是可以避免的,好的重载设计不应该出现类型转换。
 
11、
     f(int *);
     f(int *const); // redeclaration
这个问题在于,重载允许指向常量的指针,而不是指针常量,下面就是正确的:
     f(int *);
     f(const int *); // redeclaration
 
12、假设有:
typedef bool (*cmpFcn)(const string &, const string &);
bool lengthCompare(const string &, const string &);
那么:
     cmpFcn pf1 = lengthCompare;
     cmpFcn pf2 = &lengthCompare;
等价,并且
     pf("hi", "bye");           
     (*pf)("hi", "bye");       
也等价
     void useBigger(const string &, const string &,
                    bool(const string &, const string &));
     void useBigger(const string &, const string &,
                    bool (*)(const string &, const string &));
第三个参数都为函数指针,后者显式说明,等价!
 
13、当函数返回值类型为函数指针时,从内而外对其进行解析:
int (*ff(int))(int*, int);
首先,ff(int)是一个函数,参数是int,而返回值是int (*)(int*, int)
这样就清楚多了,也可以使用typedef来使得程序更加易读
typedef int (*PF)(int*, int);
PF ff(int);
 
14、在返回值中,不能返回函数类型,而只能返回指向函数的指针
 
第7章结束!!
 
---end--- next: 8.1. An Object-Oriented Library
time:05-10-8 5:27pm
 
输入某年某月某日,判断这一天是这一年的第几天?:【上一篇】
关于汉诺塔:【下一篇】
【相关文章】
  • 为什么我们的教材还不是ISO-C++标准呢?
  • 翻译:Effective C++, 3rd Edition, Item 34: 区分 inheritance of interface(接口继承)和 inheritance o...
  • 什么是真正学会C++语言
  • 一个简单的文本编辑器。(是在DEV C++下写的)
  • VC++实现声音播放
  • Dev C++ 态度[zz]
  • c/c++ reference ,很全面
  • 第1章 C++引言
  • c++学习笔记
  • 《超越C++标准库:Boost库导引》:前言
  • 【随机文章】
  • Ghost V8.0 使用详解
  • C语言编程常见问题解答之函数
  • javax.faces.el.PropertyNotFoundException
  • Windows各版本号全解释
  • vxvm rootdisk 封装和解除封装
  • 是男人应该看看
  • Ie浏览器打不开二级页面
  • Linux服务器重起步骤
  • appfuse开读5
  • 防范JAVA内存泄漏解决方案
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.