几天前
,在程序中遇到一个问题
,当我检查程序错误时
,在STL实现中发现了类似下面的代码
:
#include <iostream>
using namespace std ;
class A
{
public :
A ( const string & s )
{
cout << s .c_str () << endl ;
}
};
int main ()
{
const char * psz = "abc" ;
A a (psz );
return 0 ;
}
对此 ,我感到很奇怪 ,因为我从来没有这样做过 ,怎么能把一个引用指向一个与引用本身类型不同的类型呢 ?于是 ,我在网上发了一个帖子 ,很快 ,有人从CPL 3rd中摘录了以下内容作为回复 :
// Begin
Initialization of a reference is trivial when the initializer is an
lvalue (an object whose address you can take ; see § 4.9.6 ). The
initializer for a 'plain' T & must be an lvalue of type T . The
initializer for a const T & need not be an lvalue or even of type T .
In such cases (David ''s notes : and only in such cases ),
[ 1 ] first , implicit type conversion to T is applied if necessary (see §C .6 );
[ 2 ] then , the resulting value is placed in a temporary variable of type T ; and
[ 3 ] finally , this temporary variable is used as the value of the initializer .
Consider :
double & dr = 1 ; // error: lvalue needed
const double & cdr = 1 ; // ok
The interpretation of this last initialization might be :
double temp = double ( 1 ) ; // first create a temporary with the right value
const double & cdr = temp ; // then use the temporary as the initializer for cdr
A temporary created to hold a reference initializer persists until
the end of its reference’s scope .
References to variables and references to constants are distinguished
because the introduction of a temporary in the case of the variable
is highly errorprone ;
an assignment to the variable would become an assignment to the –
soon to disappear – temporary . No such problem exists for references
to constants , and references to constants are often important as
function arguments (§ 11.6 ).
// End
也就是说 ,只要存在可行的转换路径 ,编译器在构造引用的时候会创建一个与引用类型类型信息相同的临时变量 ,然后 ,用引用指向该临时变量 .同时 ,上面这段文字还给了我们如下信息 :
1.非 const reference不会享受这样的特殊待遇 .如下面的代码是错误的 :
float f = 0 ;
double & d = f ;
char * psz = "abc" ;
string & rstr = psz ;
(注 : VC6在编译以上各句时报告了一个与实际不符的错误提示 :A reference that is not to 'const' cannot be bound to a non -lvalue ,这里的f和psz明明是左值 )
2. const reference会使函数的适用性更强 ,但代价是在进行类型转换时可能引入较大的开销 ,失去引用传递的高效性 .
当然 ,除此之外 , const只是一个编译期的概念 ,跟 public / protected / private等一样 ,只是告诉编译器 ,我们需要针对这些标示之后的东西添加一些附加的描述 ,请区别对待 .这些描述对于 private而言 ,是说 :进行语法检查时如果发现非本类或 friend访问了这些成员 (方法 ),则报告错误 ;对于 const而言 ,则是说 :如果语法检查时发现有任何操作对 const所修饰的对象进行了修改 ,则报告错误 .
在语法检查之后 ,这些修饰符就该悄然引退了 .
#include <iostream>
using namespace std ;
class A
{
public :
A ( const string & s )
{
cout << s .c_str () << endl ;
}
};
int main ()
{
const char * psz = "abc" ;
A a (psz );
return 0 ;
}
对此 ,我感到很奇怪 ,因为我从来没有这样做过 ,怎么能把一个引用指向一个与引用本身类型不同的类型呢 ?于是 ,我在网上发了一个帖子 ,很快 ,有人从CPL 3rd中摘录了以下内容作为回复 :
// Begin
Initialization of a reference is trivial when the initializer is an
lvalue (an object whose address you can take ; see § 4.9.6 ). The
initializer for a 'plain' T & must be an lvalue of type T . The
initializer for a const T & need not be an lvalue or even of type T .
In such cases (David ''s notes : and only in such cases ),
[ 1 ] first , implicit type conversion to T is applied if necessary (see §C .6 );
[ 2 ] then , the resulting value is placed in a temporary variable of type T ; and
[ 3 ] finally , this temporary variable is used as the value of the initializer .
Consider :
double & dr = 1 ; // error: lvalue needed
const double & cdr = 1 ; // ok
The interpretation of this last initialization might be :
double temp = double ( 1 ) ; // first create a temporary with the right value
const double & cdr = temp ; // then use the temporary as the initializer for cdr
A temporary created to hold a reference initializer persists until
the end of its reference’s scope .
References to variables and references to constants are distinguished
because the introduction of a temporary in the case of the variable
is highly errorprone ;
an assignment to the variable would become an assignment to the –
soon to disappear – temporary . No such problem exists for references
to constants , and references to constants are often important as
function arguments (§ 11.6 ).
// End
也就是说 ,只要存在可行的转换路径 ,编译器在构造引用的时候会创建一个与引用类型类型信息相同的临时变量 ,然后 ,用引用指向该临时变量 .同时 ,上面这段文字还给了我们如下信息 :
1.非 const reference不会享受这样的特殊待遇 .如下面的代码是错误的 :
float f = 0 ;
double & d = f ;
char * psz = "abc" ;
string & rstr = psz ;
(注 : VC6在编译以上各句时报告了一个与实际不符的错误提示 :A reference that is not to 'const' cannot be bound to a non -lvalue ,这里的f和psz明明是左值 )
2. const reference会使函数的适用性更强 ,但代价是在进行类型转换时可能引入较大的开销 ,失去引用传递的高效性 .
当然 ,除此之外 , const只是一个编译期的概念 ,跟 public / protected / private等一样 ,只是告诉编译器 ,我们需要针对这些标示之后的东西添加一些附加的描述 ,请区别对待 .这些描述对于 private而言 ,是说 :进行语法检查时如果发现非本类或 friend访问了这些成员 (方法 ),则报告错误 ;对于 const而言 ,则是说 :如果语法检查时发现有任何操作对 const所修饰的对象进行了修改 ,则报告错误 .
在语法检查之后 ,这些修饰符就该悄然引退了 .