c++引用和const 用法 数组 指针

  非const引用,只能用object为其赋值; 《c++primer》P52

  而const引用则可以用临时变量为其赋值;

  如:

  const int &r = 32;//可以

   int &rr = 32 // error

而且:

非const引用只能绑定到与该引用同类型的对象;

const引用则可以绑定到不同但相关的类型的对象或绑定到右值;//左值:可以出现在赋值语句的坐标或右边;右值:只能出现在赋值的右边。

 

当进行string对象和字符串字面值混合连接操作是,+操作符的左右操作数必须至少有一个是string类型

1、

字符指针数组创建:

char **parr = new char*[n];

。。。。

//释放各个字符数组

for(i = 0; i != n; ++i)

  delete [] parr[i];

// 释放字符指针数组

delete [] parr;

2、《c++ primer》P117

动态分配数组是,如果数组元素具有类类型,将用该类的默认构造函数实现初始化,如果数组元素是内置类型,则无初始化:

string *psa = new string[10];   //array of empty strings

int *pia = new int[10];    //array of uninitialized ints

 

//因为数组元素都是const对象,无法赋值。所以只能对数组做值初始化,《C++primer 》P118

 int const *par = new const int[10]; //错误,没有初始化;

 int const *par = new const int[10]();// 正确,初始化了,

C++允许定义类类型的const数组,但该类类型必须提供默认构造函数;

const string *p = new const string[10];//正确,array of 10 empty strings。

C++ 虽然不准定义长度为0的数组变量,但是明确指出,调用new动态穿件长度为0的数组是合法的。

char arr[0];    //error 

char *p = new char [0];    //ok

 

问题:

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9         vector<string> vec;
10         string str;
11         cout << "please some strings end up with (Ctrl + D):\n";
12         while(cin >> str)
13                 vec.push_back(str);
14 
15         char **parr = new char*[vec.size()];  
16         size_t i = 0;
17         for(vector<string>::const_iterator ite = vec.begin(); ite != vec.end(); ++ite, ++i)
18         {    
19                 cout << *ite << "\t";
20                 char *p = new char[(*ite).size() + 1]; 
21                 strcpy(p, (*ite).c_str());
22                 //strcpy(parr[i], (*ite).c_str());
23                 parr[i] = p;
24         }
25         cout << endl;
26         for(size_t i = 0; i != vec.size(); ++i)
27         {
28                 cout << parr[i];
29                 delete [] parr[i];
30         }
31         delete [] parr;
32 
33         return 0;
34 }   

 

上面代码可以正常运行;

但是注释掉20,21,23行,用23行,则提示段错误,

即使把15行改为下面的样子也不行:

char **parr = new char*[vec.size()]();

可见不能用

strcpy(parr[i], (*ite).c_str());

却可以用21行:

  strcpy(p, (*ite).c_str());

总结:

当用new建立指针(*P)时,分配了空间,而且分配的空间是没有用的,

但是建立指针额指针(**p)是,内存指针的指向是不定的,也许是系统真用的,所以冒然用strcpy(p[i],string.c_str())会把原来的内容代替,

造成段错误。所以不可以那样用。

而且string.c_str()返回的是const char类型的数组,不可以赋值给非const的数组或者指针。

字符串字面值得类型就是const char 类型的数组----《c++primer》P113

3、多维数组和指针:

int ia[3][4];  //array of size 3,each element is an array of ints of size 4

int (*ip)[4];  //ip points to an array of 4 ints

ip  = &ia[2];  //ia[2]is an array of 4 ints

 

int *ip[4];   //array of points to int

int (*ip)[4]; // pointer to an array of 4 ints

 

 

typedef int int_array[4];

int_array *ip = ia; 

 

for(int_array *p = ia; p != ia+3; ++p)

  for(int *q = *p; q != *p + 4; ++q)

    cout << *q << endl;

 

 

typedef string *pstring;

const pstring cstr;

上面等价于:

string *const cstr;

 

下面两个也等价:

string const s1;

const string s2;

///////////////////////  《C++ primer》P112

string s;

typedef string *pstring;

const pstring cstr1 = &s;//这三个等价,都是const指针;

pstring const cstr2 = &s;

string *const cstr3 = &s;

 

posted on 2013-07-26 22:18  yi_meng  阅读(1961)  评论(0编辑  收藏  举报

导航