Posted on 2010-12-06 22:17
Sivan 阅读(501)
评论(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)返回值引用传递,一般只出现在类的赋值函数中,实现链式表达。
1
class A
2![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
A & operator = (const A &other); // 赋值函数
4
}
5![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
1.3 const成员函数
任何不会修改数据成员的函数都应该声明为const类型。
规则:
1)const对象只能访问const成员函数,而非const对象可以访问任意的成员函数,包括const成员函数。
2)const对象的成员是不可修改的,而const对象通过指针维护的对象却是可以修改的。
3)const成员函数不可以修改对象的数据,不管对象是否具有const性质。
4)mutable修饰的数据成员,对通过任何手段都可以修改,const成员函数也可以修改。
1
class A
2![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
public:
4![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
A(int i):m_nVal(i)
{m_pVal = new int();}
5![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void Print()
{cout<<m_nVal<<" "<<*m_pVal<<endl;}
6![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void Print() const
{cout<<"const print:"<<m_nVal<<" "<<*m_pVal<<endl;}
7
int* m_pVal;
8
int m_nVal;
9
};
10![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
11
int main()
12![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
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![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
20![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
2.表驱动
以前使用过表驱动方法,但是并不知道这就是表驱动。
表驱动方法是一种可以在表中查找信息,而不必使用很多逻辑语句(if,switch case)来找出的方法。
2.1 逻辑性不强
例:
1
void GetWeekDays(string & strDay, int iDay)
2![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(1 == iDay)
{strDay = "Monday";}
4![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if(2 == iDay)
{strDay = "Tuesday";}
5![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if(3 == iDay)
{strDay = "Wednesday";}
6![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if(4 == iDay)
{strDay = "Thursday";}
7![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if(5 == iDay)
{strDay = "Friday";}
8![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if(6 == iDay)
{strDay = "Saturday";}
9![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if(7 == iDay)
{strDay = "Sunday";}
10
return;
11
}
12![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
表驱动方法
1![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
static string strWeekDays[] =
{"Monday","Tuesday","Wednesday",
2
"Thursday","Friday","Saturday","Sunday"};
3![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
4
void GetWeekDays2(string & strDay, int iDay)
5![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
6
strDay = strWeekDays[iDay-1];
7
}
8![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
9![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
2.2 函数指针在表驱动方法中应用
不同分支调用不同的函数
1
void FunA(string strType)
2![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
if (strType == "1")
4![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
DoFun1();
6
}
7
if (strType == "2")
8![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
DoFun1();
10
}
11
if (strType == "3")
12![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
DoFun1();
14
}
15
if (strType == "4")
16![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
DoFun1();
18
}
19
if (strType == "5")
20![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
DoFun1();
22
}
23
if (strType == "6")
24![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
DoFun1();
26
}
27
if (strType == "7")
28![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
DoFun1();
30
}
31
}
表驱动方法
1
typedef struct
2![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
string strType;
4
void (*Fun)(void);
5
}DoTable;
6![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
7![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
static const DoTable Table[]=
{
8![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{"1",DoFun1},
9![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{"2",DoFun2},
10![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{"3",DoFun3},
11![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{"4",DoFun4},
12![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{"5",DoFun5},
13![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{"6",DoFun6},
14![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{"7",DoFun7}
15
}
16
}
17![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
18
void FunA(string strType)
19![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
20
int nCount = sizeof(Table)/sizeof(Table[0]);
21
for (int i=0; i!=nCount; ++i)
22![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
if (Table[i] == strType)
24![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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