Sivan's blog

用代码说话……
posts - 14, comments - 2, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

[学]const及表驱动

Posted on 2010-12-06 22:17 Sivan 阅读(465) 评论(0)  编辑 收藏 引用 所属分类: C/C++

看了《金山卫士代码批评》一文,发现了一些自己存在的问题。
1.const使用
1.1 const修饰参数
1)参数为输出参数,不论什么数据类型,不论什么参数传递方式,都不加const。
2)输入参数“指针传递”,加const防止改动指针。
3)输入参数“值传递”,内置类型,参数不加const修饰,非内置类型,常使用引用传递,如果不想改变参数,加const修饰,如void Fun(const A & a)
1.2 const修饰函数返回值
1)返回值“指针传递”,加const修饰,函数返回值内容不能被修改,返回值只能赋给加const修饰的同类型指针。
2)返回值“值传递”,加const修饰无意义。
3)返回值引用传递,一般只出现在类的赋值函数中,实现链式表达。

1class A
2{
3 A & operator = (const A &other); // 赋值函数
4}

5


1.3 const成员函数
任何不会修改数据成员的函数都应该声明为const类型。
规则:
1)const对象只能访问const成员函数,而非const对象可以访问任意的成员函数,包括const成员函数。
2)const对象的成员是不可修改的,而const对象通过指针维护的对象却是可以修改的。
3)const成员函数不可以修改对象的数据,不管对象是否具有const性质。
4)mutable修饰的数据成员,对通过任何手段都可以修改,const成员函数也可以修改。

 1class A
 2{
 3public:
 4 A(int i):m_nVal(i){m_pVal = new int();}
 5 void Print(){cout<<m_nVal<<" "<<*m_pVal<<endl;}
 6 void Print() const {cout<<"const print:"<<m_nVal<<" "<<*m_pVal<<endl;}
 7 int* m_pVal;
 8 int m_nVal;
 9}
;
10
11int main()
12{
13 const A a(1);
14 a.Print();         // 规则1,const对象只能防伪const成员函数
15    //a.m_nVal = 2;    // 规则2,const对象的成员不可修改
16 *(a.m_pVal) = 3;   // 规则2,const对象通过指针维护的对象可以修改
17 return 0;
18}

19
20

 

2.表驱动
以前使用过表驱动方法,但是并不知道这就是表驱动。
表驱动方法是一种可以在表中查找信息,而不必使用很多逻辑语句(if,switch case)来找出的方法。
2.1 逻辑性不强
例:

 1void GetWeekDays(string & strDay, int iDay)
 2{
 3 if(1 == iDay){strDay = "Monday";}
 4 else if(2 == iDay){strDay = "Tuesday";}
 5 else if(3 == iDay){strDay = "Wednesday";}
 6 else if(4 == iDay){strDay = "Thursday";}
 7 else if(5 == iDay){strDay = "Friday";}
 8 else if(6 == iDay){strDay = "Saturday";}
 9 else if(7 == iDay){strDay = "Sunday";}
10 return;
11}

12


表驱动方法

1static string strWeekDays[] = {"Monday","Tuesday","Wednesday",
2"Thursday","Friday","Saturday","Sunday"}
;
3
4void GetWeekDays2(string & strDay, int iDay)
5{
6 strDay = strWeekDays[iDay-1];
7}

8
9


2.2 函数指针在表驱动方法中应用
不同分支调用不同的函数

 1void FunA(string strType)
 2{
 3    if (strType == "1")
 4    {
 5        DoFun1();
 6    }

 7    if (strType == "2")
 8    {
 9        DoFun1();
10    }

11    if (strType == "3")
12    {
13        DoFun1();
14    }

15    if (strType == "4")
16    {
17        DoFun1();
18    }

19    if (strType == "5")
20    {
21        DoFun1();
22    }

23    if (strType == "6")
24    {
25        DoFun1();
26    }

27    if (strType == "7")
28    {
29        DoFun1();
30    }

31}

表驱动方法

 1typedef struct  
 2{
 3    string strType;
 4    void (*Fun)(void);
 5}
DoTable;
 6
 7static const DoTable Table[]={
 8    {"1",DoFun1},
 9    {"2",DoFun2},
10    {"3",DoFun3},
11    {"4",DoFun4},
12    {"5",DoFun5},
13    {"6",DoFun6},
14    {"7",DoFun7}
15    }

16}
17
18void FunA(string strType)
19{
20    int nCount = sizeof(Table)/sizeof(Table[0]);
21    for (int i=0; i!=nCount; ++i)
22    {
23        if (Table[i] == strType)
24        {
25            (*Table[i].Fun)();
26        }

27    }

28}



参考:
1.http://www.cnblogs.com/Fancyboy2004/archive/2008/12/23/1360810.html
2.http://www.cnblogs.com/MichaelPeng/archive/2010/12/02/1893999.html?page=1#pagedcomment
3.http://baike.baidu.com/view/3707325.htm


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理