2009年6月4日 星期四
C/C++的const總介紹
簡單的介紹C中const的用法 1 int main() 2 { 3 const char ch = 'a'; 4 const int a[5] = {1, 2, 3, 4, 5}; 5 int b[5] = {6, 7, 8, 9, 10}; 6 int c[4][4] = {{11,12,13,14},{15,16,17,18},{19,20,21,22},{23,24,25,26}}; 7 const int *p1 = a; 8 int *const p2 = a; 9 const int * const p3 = b; 10 const int ** const p4 = c; 11 12 ch = 'b';//error: ch is const, can't be modified 13 p1 = &a[2]; //ok, *p1 is const, p1 is not 14 *p1 = 5;//error, *p1 is const, a[2] is const 15 *(p1 + 2) = 1; //error, *p1 is const 16 p1 = &b[2];//ok 17 *p1 = 4;//error, *p1 is const 18 p2 = &b[4];//error, p2 is const 19 p2++;//error, p2 is const 20 *p2 = 10;//error, *p2 is not const, p2 is, but a[0] is const 21 p2 = &a[5];//error, p2 is const 22 p3 = &b[1];//error, p3 is const 23 *p3 = 3;//error, *p3 is const 24 p4 = b;//error, p4 is const 25 *(*p4+2) = 10; //error, **p4 is const 26 *p4 = b; 27 printf("%d", **p4); //6, *p4 = b 28 **p4 = 10;//error, **p4 is const 29 30 return 0; 31 } 32 --------------------------------------------------------------------------------------
簡單的介紹C++中const的用法 1 /* 2 * ===================================================================================== 3 * 4 * Filename: const.cpp 5 * 6 * Description: 7 * 8 * Version: 1.0 9 * Created: 06/04/2009 09:01:15 PM 10 * Revision: none 11 * Compiler: gcc 12 * 13 * Author: YOUR NAME (), 14 * Company: 15 * 16 * ===================================================================================== 17 */ 18 #include <stdio.h> 19 #include <stdlib.h> 20 class Ball { 21 static const int ID = 100;//initialize only once, can't modify anymore 22 //const int ID2; //can't modify it anymore, and initialize 23 char *name; 24 int money; 25 mutable int bonus; 26 public: 27 Ball(){ 28 name = (char *)malloc(8); 29 name[0] = 'y'; 30 name[1] = 's'; 31 } 32 int setID(int id){ 33 //ID = 200;//error, ID is const 34 } 35 void showID(){ 36 printf("ID:%d\n", ID); 37 } 38 const char* getConstName() { return name; } 39 char* getName() { return name; } 40 int getMoney() const { 41 //money++;//error, function is const, can't modify any its class member 42 bonus++;//ok, it is mutable, can be used in const function 43 return money+bonus; } 44 }; 45 int main() 46 { 47 Ball b = Ball(); 48 b.showID(); 49 printf("name:%s\n", b.getName()); 50 b.getName()[2]='l'; 51 printf("name:%s\n", b.getName()); 52 //b.getConstName()[2]='l';//error, return const can't modify 53 printf("money:%d\n", b.getMoney()); 54 55 return 0; 56 } 57 --------------------------------------------------------------------------------------
簡單的介紹C++中const templates的iterator用法,還有其中content為const時如何去掉const來呼叫 1 #include <iostream> 2 #include <vector> 3 #include <map> 4 #include <string> 5 6 using namespace std; 7 8 class Symbolic{ 9 public: 10 Symbolic(int v, string e){ 11 value_ = v; 12 expr_ = e; 13 } 14 15 void Print(){ 16 cout<<"value:"<<value_<<", "<<expr_<<endl; 17 } 18 19 void constPrint() const{ 20 cout<<"value:"<<value_<<", "<<expr_<<endl; 21 } 22 private: 23 int value_; 24 string expr_; 25 }; 26 27 void vectorPrint(const vector<const Symbolic *>& v) 28 { 29 /* const_iterator for const template iteration */ 30 vector<const Symbolic *>::const_iterator v_iter; 31 for(v_iter = v.begin(); v_iter != v.end(); v_iter++){ 32 /* make const member to be normal */ 33 const_cast<Symbolic *>(*v_iter)->Print(); 34 /* const class pointer only can call const function */ 35 (*v_iter)->constPrint(); 36 } 37 38 } 39 40 void mapPrint(const map<int, char>& m) 41 { 42 /* const_iterator for const template iteration */ 43 map<int, char>::const_iterator m_iter; 44 for(m_iter = m.begin(); m_iter != m.end(); m_iter++){ 45 cout<<"index:"<<m_iter->first<<" -> "<<m_iter->second<<endl; 46 } 47 } 48 49 int main() 50 { 51 vector<Symbolic *> constraints; 52 53 constraints.push_back(new Symbolic(1, "x")); 54 constraints.push_back(new Symbolic(2, "y")); 55 constraints.push_back(new Symbolic(3, "z")); 56 57 /* make its member to be const access */ 58 vector<const Symbolic*> cs(constraints.begin(), 59 constraints.end()); 60 /* print its value and make it const to be sure that we don't modify it */ 61 vectorPrint(cs); 62 63 64 map<int, char> symb; 65 66 symb[1] = 'a'; 67 symb[2] = 'b'; 68 symb[3] = 'c'; 69 70 mapPrint(symb); 71 72 return 0; 73 }
訂閱:
張貼留言 (Atom)
Labels
- blog config (2)
- c/c++ (4)
- perl (1)
- vim (2)
- visual c++ (3)
0 意見:
張貼留言