C++笔记

----------------------------------------------

//ok

int ival( 1024 );

int ival(int());

//每种内置数据类型都支持一种特殊的构造函数语法可将对象初始化为0

int ival=int();

-------------------------------------------------

试图将一个非const 对象的指针指向一个常量对象的动作都将引起编译错误,

const 对象的地址只能赋值给指向const 对象的指针, 但是指向const 对象的指针可以被赋以一个非const 对象的地址

const double minWage = 9.60;
//  error!
double *ptr = &minWage;

--------------------------------------------------------

常量指针:指向常量的指针,指针所指向的地址的内容是不可修改的。

如:const int *p=&a;(p指向const int)

指针常量:指针的常量,它是不可改变地址的指针,但是可以对它所指向的内容进行修改.

如:int * const p=&a;(const p指向int)

-------------------------------------------------------------

引用类型:引用reference 有时候又称为别名alias 它可以用作对象的另一个名字。

int ival = 1024;
int &refVal = ival;

------------------------------------------------------

枚举类型用法

enum open_modes{ input = 2, output, append };
open_modes om=output;
cout<<om<<endl;

// point2d == 2, point2w == 3, point3d == 3, point3w == 4
enum Points { point2d = 2, point2w, point3d = 3, point3w };

void mumble() {
Points pt3d = point3d; // ok: pt3d == 3
// 错误pt2w 被初始化为一个int 整数
Points pt2w = 3;
// 错误polygon 不是Points 的枚举成员
pt2w = polygon;
// ok: pt2w 和pt3d 都是Points 枚举类型
pt2w = pt3d;
}
但是在必要时枚举类型会自动被提升成算术类型例如
const int array_size = 1024;
// ok: pt2w 被提升成int 类型
int chunk_size = array_size * pt2w;

----------------------------------------------------

迭代器用法

vector< string > text;

for ( vector<string>::iterator it = text.begin();
it != text.end(); ++it )
cout << *it << ' ';

//ok

int ia[ 7 ] = { 0, 1, 1, 2, 3, 5, 8 };
vector< int > ivec1(ia,ia+5) ;

------------------------------------------------------

下面是一个几乎所有人刚开始时都会答错的问题错误在于将typedef 当作宏扩展已
知下面的typedef
typedef char *cstring;
在以下声明中cstr 的类型是什么
extern const cstring cstr;
第一个回答差不多都是
const char *cstr
即指向const 字符的指针但是这是不正确的const 修饰cstr 的类型cstr 是一个指
针因此这个定义声明了cstr 是一个指向字符的const 指针见3.5 节关于const 指针类型
的讨论
char *const cstr;

------------------------------------------------------------

volatile 修饰符的主要目的是提示编译器该对象的值可能在编译器未监测到的情况下被
改变因此编译器不能武断地对引用这些对象的代码作优化处理

------------------------------------------------------------

pair 类也是标准库的一部分它使得我们可以在单个对象内部把相同类型或不同类型的
两个值关联起来为了使用pair 类我们必须包含下面的头文件
#include <utility>
例如
pair< string, string > author( "James", "Joyce" );
创建了一个pair 对象author 它包含两个字符串分别被初始化为James 和Joyce
我们可以用成员访问符号member access notation 访问pair 中的单个元素它们的名
字为first 和second 例如
string firstBook;
if ( author.first == "James" &&
author.second == "Joyce" )
firstBook = "Stephen Hero";
如果我们希望定义大量相同pair 类型的对象那么最方便的做法就是用typedef 如下所

typedef pair< string, string > Authors;
Authors proust( "marcel", "proust" );
Authors joyce( "james", "joyce" );
Authors musil( "robert", "musil" );

--------------------------------------------------------------

向前声明

可以声明一个类而不定义它
   class Screen;//declaration of the Screen class
   这个声明,有时候被称为前向声明(forward declaration),在程序中引入了类类型的Screen.在声明之后,定义之前,类Screen是一个不完全类型(incompete type),即已知Screen是一个类型,但不知道包含哪些成员.
   不完全类型只能以有限方式使用,不能定义该类型的对象,不完全类型只能用于定义指向该类型的指针及引用,或者用于声明(而不是定义)使用该类型作为形参类型或返回类型的函数.

// 前向声明(forward declaration)
class EntrySlot;
extern EntrySlot* look_up( string );
typedef pair< string, EntrySlot* > SymbolEntry;
SymbolEntry current_entry( "author", look_up( "author" ));

// ...
if ( EntrySlot *it = look_up( "editor" ))
{
current_entry.first = "editor";
current_entry.second = it;
}

--------------------------------------------------------------

操作符重载

被重载的操作符采用下面的一般形式
return_type operator op ( parameter_list );
这里的operator 是关键字op 是一个预定义的操作符如+ = == []

char& operator[]( int );
声明了一个下标操作符的重载实例它带有一个int 型的参数返回指向char 的引用

例如
// 放在程序文本文件中: String.C
// 包含String 类的定义
#include "String.h"
// 包含strcmp()函数的声明
// cstring 是标准C 库的头文件
#include <cstring>
bool // 返回类型
String:: // 说明这是String 类的一个成员
operator== // 函数的名字: 等于操作符
(const String &rhs) // 参数列表
{
if ( _size != rhs._size )
return false;
return strcmp( _string, rhs._string ) ? false : true;
}

inline String&
String::operator=( const String &rhs )
{
if ( this != &rhs )

{
delete [] _string;
_size = rhs._size;
if ( ! rhs._string )
_string = 0;
else {
_string = new char[ _size + 1 ];
strcpy( _string, rhs._string );
}
}
return *this;
}

==============

bitset< 32 > bitvec;

定义32位bitset

----------------------------------

显式转换

显式转换也被称为强制类型转换cast 包括下列命名的强制类型转换操作符
static_cast dynamic_cast const_cast 和reinterpret_cast 虽然有时候确实需要强制类型转
换但是它们也是程序错误的源泉通过使用它们程序员关闭了C++语言的类型检查设
施在了解怎样把一个值从一种类型强制转换成另一种类型之前我们先来看一下何时需
要这么做
任何非const 数据类型的指针都可以被赋值给void*型的指针,void*型指针被用于对象的确切类型未知或者在特定环境下对象的类型会发生变化的情况下有时void*型的指针被称为泛型generic 指针因为它可以指向任意数据类型的指针

int ival;
int *pi = 0;
char *pc = 0;
void *pv;
pv = pi; // ok: 隐式转换
pv = pc; // ok: 隐式转换
const int *pci = &ival;
pv = pci; // 错误: pv 不是一个const void*.
const void *pcv = pci; // ok

把void*型的指针赋值给任意显式类型时C++要求显式强制转换

----------------------------------------------------------------------------------------

函数指针

#include <iostream>
int min( int*, int );
int (*pf)( int*, int ) = min;
const int iaSize = 5;
int ia[ iaSize ] = { 7, 4, 9, 2, 5 };
int main() {
cout << "Direct call: min: "

<< min( ia, iaSize ) << endl;
cout << "Indirect call: min: "
<< pf( ia, iaSize ) << endl;
return 0;
}
int min( int* ia, int sz ) {
int minVal = ia[ 0 ];
for ( int ix = 1; ix < sz; ++ix )
if ( minVal > ia[ ix ] )
minVal = ia[ ix ];
return minVal;
}
调用
pf( ia, iaSize );
也可以用显式的指针符号写出
(*pf)( ia, iaSize );

我们可以声明一个函数指针的数组例如
int (*testCases[10])();
将testCases 声明为一个拥有10 个元素的数组,每个元素都是一个指向函数的函数指针
该函数没有参数返回类型为int
像数组testCases 这样的声明非常难读因为很难分析出函数类型与声明的哪部分相关
在这种情况下使用typedef 名字可以使声明更为易读例如
// typedefs 使声明更易读
typedef int (*PFV)(); // 定义函数类型指针的typedef
PFV testCases[10];
testCases 的这个声明与前面的等价

[]优先级大于*,*是自右向左结合

指针数组,顾名思义,就是说的首先是一个数组吧,然后数组的元素是指针而已。

说明形式为:type *pointer_array[constant1][constant2]...[constantn];

数组指针:指向一个数组的指针。说明形式为:type (*pointer_array)[constant1][constant2]...[constantn];int (*ap)[2]; 这样就说明了一个指向包含有2个元素的整形数组的数组指针

-------------------------------------------------------

函数模板

template <class Type>
Type min( Type a, Type b ) {
return a < b ? a : b;
}

template <typename Type, int size>
Type min( Type (&r_array)[size] )
{
Type min_val = r_array[0];
for ( int i = 1; i < size; ++i )
if ( r_array[i] < min_val )
min_val = r_array[i];
return min_val;
}

int ia[] = { 10, 7, 14, 3, 25 };
int i = min( ia );

----------------------------------------------------

 virtual void finalize_state() = 0;//纯虚函数,必须在子类中实现

virtual void finalize_state();//虚函数

---------------------------------------------

string str() const;

函数声明后加const表示该函数不能在函数实现中更改成员变量的值

原文地址:https://www.cnblogs.com/yangyh/p/1709277.html