C++Primer Dialy

1. 一些常量定义为Const,const int count = 10;/const double *p=0;则p的地址可变但不能改变其指向的东西的值。int strcmp( const char *str1, const char *str2 );
 int i = -1;对
 const int ic = i;对
 const int *pic = ⁣对
 int errNumb = 0;对
 int *const curErr = &errNumb;对
// int *const cpi = ⁣错,因为这样的定义表示可以用cpi来修改ic的值了,事实上ic是const的
// const int *const cpic = ⁣对
2. complex,vector,string,vector,<algorithm>头文件。在用vector,sort,string,Pair和complex等时前用std::修饰,或者用using std::vector;using std::sort;等来引入命名空间,或者using namespace std;
3. inti = int()/int(12),string.operator +=("~~");而不可直接+=,char *cp = "12345";str1 = cp;,而不是*cp
4. // point2d == 2, point2w == 3, point3d == 3, point3w == 4
    enum Points { point2d = 2, point2w, point3d = 3, point3w };
5. template<class elemType>
 void myswap2(elemType &a1,elemType &a2)
 {
  elemType temp = a2;
  a2 = a1;
  a1 = temp;
 }
6. namespace my_namespace_name {class or function or else}
7. 只有返回值不同的函数不能成为重载,而如果在两个函数的参数表中只有缺省实参不同则第二个声明被视为第一个的重复声明例如
 // 声明同一函数
 int max( int *ia, int sz );
 int max( int *, int = 10 );

 // 声明了不同的函数
 void f( int* );
 void f( const int* );
 // 也声明了不同的函数
 void f( int& );
 void f( const int& );
8. template <class Parm, class U>
 Parm minus( Parm* array, U value )
 {
  typename Parm::name * p; // ok: 指针声明
 }//注意typename关键字,若无,则不法知道param::name *p是指针还是乘法。
9. template <typename Type, int size>
 Type min( Type (&r_array)[size] )
 {
 }
 int ia[] = { 10, 7, 14, 3, 25 };
 int i = min( ia );
 int (*pf)(int (&)[10]) = &min;
10. class Text {
 public:
 void bad( const string &parm ) const;
 private:
 char *_text;
 };
 void Text::bad( const string &parm ) const
 {
  _text = parm.c_str(); // 错误: 不能修改 _text
  for ( int ix = 0; ix < parm.size(); ++ix )
  _text[ix] = parm[ix]; // 不好的风格, 但不是错误的
 }
 //mutable 易变
11. 通过 classname& function() {return *this;}来实现classname cn;cn.function().function().function();
12. 类的static成员变量为所有该类的实例所共有一个拷贝。表态成员函数不能定义为静态,定义里用static,实现时不用加,表态函数 不能用this指针。
13. int (CBeing::*pgetage)();
 pgetage = CBeing::GetAge;//没有()
 (dad.*pgetage)();
14. 一般来说通过指针或引用向一个函数传递一个类对象比传值更有效率例如函数原型bool sufficient_funds{ Account acct, double };
要求每个调用都用实际被传递进来的Account 对象按成员初始化参数acct 下面是修订后的函数版本bool sufficient_funds( const Account &acct, double );它只要求拷贝Account 对象的地址值而不会发生类的初始化操作关,注意使用const的意义。
15. 用new来的对象指针必须用delete来处理,否则会产生内存泄漏。
16. 对于const 对象只有const 非静态成员函数才可以被调用。
17. 用基类的指针或引用操纵多个类型的能力被称为多态polymorphism。
18. void*指针?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值