| flw 回复于:2003-08-20 12:36:53
|
已收入精华。
|
| quanliking 回复于:2003-08-20 19:37:11
|
谢谢!
|
| jobman 回复于:2003-08-21 00:13:43
|
有几点不同意见:
[code:1:3186d5e9be]预处理语句虽然有以上的许多优点,但它有个比较致命的缺点,即,预处理语句仅仅只是简单值替代,缺乏类型的检测机制。这样预处理语句就不能享受c++严格类型检查的好处,从而可能成为引发一系列错误的隐患。 [/code:1:3186d5e9be]
宏定义是在预处理时完成的,可是依然要通过编译器,不可能躲过类型
检查,例如:
[code:1:3186d5e9be]#define MY_MACRO "fsdfsdfdsdf"
int func1( int parameter )
{
.....
}
main( )
{
func1( MY_MACRO );
}[/code:1:3186d5e9be]
这段代码肯定无法编译通过,所以这段描述不成立。
宏定义的使用技巧也是博大精深的,绝非 const 能替代,
当然在具有值替换的场合,用 const 来代替宏定义是个不错的
选择,可也仅此而已,而且用宏定义并不会引入类型隐患。
const 的引入其主要目的并不在于代替宏定义,这多少有点牵强了。
|
| 小飞爱使申华 回复于:2003-08-21 03:23:02
|
[quote:fb2fd29f5e="quanliking"]谢谢![/quote:fb2fd29f5e]
马甲穿错了吧,^_^
|
| clion 回复于:2003-08-21 20:46:49
|
这个帖子很好
|
| aero 回复于:2003-08-21 21:00:31
|
[quote:e4afadc072="jobman"]
这段代码肯定无法编译通过,所以这段描述不成立。
宏定义的使用技巧也是博大精深的,绝非 const 能替代,
当然在具有值替换的场合,用 const 来代替宏定义是个不错的
选择,可也仅此而已,而且用宏定义并不会..........[/quote:e4afadc072]
呵呵,两位说的都对。但是编译器对类型的检查是发生在int func1( int parameter ) 的,而不是在#define MY_MACRO "fsdfsdfdsdf"
。所以原文说的还是没错,jobman的意思也对。
|
| HappyWin 回复于:2003-08-24 16:46:11
|
精华,收藏先
|
| 天上的小星星 回复于:2004-02-10 11:44:07
|
好贴
|
| whyglinux 回复于:2004-04-10 03:51:16
|
[quote:edc740afb5="yuxq"]...
5. const 限定类的成员函数:
class classname {
public:
int fun() const;
.....
}
注意:采用此种const 后置的形式是一种规定,亦为了不引起混淆。在此函数的声明中和定义中均要使用const,因为const已经成为类型信息的一部分。
获得能力:可以操作常量对象。
失去能力:不能修改类的数据成员,不能在函数中调用其他不是const的函数。
[/quote:edc740afb5]
楼主的这篇文章值得仔细阅读。但是,我觉得上述“const 限定类的成员函数”这一部分写得比较简略,特别是其中“注意”后面的文字,更是使人不知所云,所以想对这一部分做一些补充说明。
类的成员函数后面加 const,表明这个函数不会对这个类对象的数据成员(准确地说是非静态数据成员)作任何改变。在设计类的时候,一个原则就是对于不改变数据成员的成员函数都要在后面加 const,而对于改变数据成员的成员函数不能加 const。所以 const 关键字对成员函数的行为作了更加明确的限定:有 const 修饰的成员函数(指 const 放在函数参数表的后面,而不是在函数前面或者参数表内),只能读取数据成员,不能改变数据成员;没有 const 修饰的成员函数,对数据成员则是可读可写的。除此之外,在类的成员函数后面加 const 还有什么好处呢?楼主告诉我们的:“获得能力:可以操作常量对象”,其实应该是常量(即 const)对象可以调用 const 成员函数,而不能调用非const修饰的函数。正如非const类型的数据可以给const类型的变量赋值一样,反之则不成立。
请看下面一个完整的例子,然后我再作一些说明。
[code:1:edc740afb5]
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
Student() {}
Student( const string& nm, int sc = 0 )
: name( nm ), score( sc ) {}
void set_student( const string& nm, int sc = 0 )
{
name = nm;
score = sc;
}
const string& get_name() const
{
return name;
}
int get_score() const
{
return score;
}
private:
string name;
int score;
};
// output student's name and score
void output_student( const Student& student )
{
cout << student.get_name() << "\t";
cout << student.get_score() << endl;
}
int main()
{
Student stu( "Wang", 85 );
output_student( stu );
}
[/code:1:edc740afb5]
设计了一个类 Student,数据成员有 name 和 score,有两个构造函数,有一个设置成员数据函数 set_student(),各有一个取得 name 和 score 的函数 get_name() 和 get_score()。请注意 get_name() 和 get_score() 后面都加了 const,而 set_student() 后面没有(也不能有const)。
首先说一点题外话,为什么 get_name() 前面也加 const。如果没有前后两个 const 的话,get_name() 返回的是对私有数据成员 name 的引用,所以通过这个引用可以改变私有成员 name 的值,如
[code:1:edc740afb5] Student stu( "Wang", 85 );
stu.get_name() = "Li";
[/code:1:edc740afb5]
即把 name 由原来的 "Wang" 变成了 "Li",而这不是我们希望的发生的。所以在 get_name() 前面加 const 避免这种情况的发生。
那么,get_name() 和 get_score() 这两个后面应该加 const的成员函数,如果没有 const 修饰的话可不可以呢?回答是可以!但是这样做的代价是:const对象将不能再调用这两个非const成员函数了。如
[code:1:edc740afb5]const string& get_name(); // 这两个函数都应该设成 const 型
int get_score();
void output_student( const Student& student )
{
cout << student.get_name() << "\t"; // 如果 get_name() 和 get_score() 是非const成员函数,这一句和下一句调用是错误的
cout << student.get_score() << endl;
}
[/code:1:edc740afb5]
由于参数student表示的是一个对const Student型对象的引用,所以 student 不能调用非const成员函数如 set_student()。如果 get_name() 和 get_score() 成员函数也变成非const型,那么上面的 student.get_name() 和 student.get_score() 的使用就是非法的,这样就会给我们处理问题造成困难。
因此,我们没有理由反对使用const,该加const时就应该加上const,这样使成员函数除了非const的对象之外,const对象也能够调用它。
|