注册 登录
编程论坛 C++教室

reference& 作用

vfdff 发布于 2008-09-04 08:31, 858 次点击
template <typename Block, typename Allocator>
class dynamic_bitset
{
public:
    typedef Block block_type;
    typedef implementation-defined size_type;
    enum { bits_per_block = CHAR_BIT * sizeof(Block) };

    class reference
    {
    public:
        // An automatically generated copy constructor.
        reference& operator=(bool value);
        reference& operator|=(bool value);
        reference& operator&=(bool value);
        reference& operator^=(bool value);
        reference& operator-=(bool value);
        reference& operator=(const reference& j);
        reference& operator|=(const reference& j);
        reference& operator&=(const reference& j);
        reference& operator^=(const reference& j);
        reference& operator-=(const reference& j);
        bool operator~() const;
        operator bool() const;
        reference& flip();
    };
    typedef bool const_reference;

看到库文件中的bitset 第一中由上述语句,看不懂定义这个class reference有什么作用?
7 回复
#2
xlh52252008-09-05 20:39
不知道,你在问什么!
#3
vfdff2008-09-05 23:52
回复 2# xlh5225 的帖子
operator前为什么定义类型为  reference&  ?
#4
中学者2008-09-05 23:53
与内建类型兼容...因为对于int a,b,c;  (a=b)=c或 (a^=b)=(a+c)这样的运算是可以的..
#5
vfdff2008-09-06 00:38
回复 4# 中学者 的帖子
您的意思是说如果没有  reference&  
则 无法 进行 (a=b)=c或 (a^=b)=(a+c)这样的运算?

补充:

C++数据类型包括:  
  1)内置的基本数据类型,如整型、浮点型、布尔型等,均有一个关键字对应,如int,float,bool  
  2)C++   STL(标准库)引入了一些扩展类型,有时候也归为基本类型,比如字符串类型(string),复数类型(complex),向量(vector)等。  
  3)其他,即用户自定义类型,也叫抽象数据类型(ADT),即用户通过class,struct,enum定义的各种数据类型。
内建类型是指C++所支持的基本类型,而用户自定义类型是程序员或者开发商定义的类型,可能是类、或者是struct、enum

[[it] 本帖最后由 vfdff 于 2008-9-20 20:24 编辑 [/it]]
#6
中学者2008-09-06 00:50
yes...当然你直接返回值也可以..但是这样做就没意义了
#7
sunkaidong2008-09-06 14:46
没听过,学习了
#8
vfdff2008-09-20 20:27
回复 6# 中学者 的帖子
还是没有明白,那为什么如此定义后,就能使这些定义类也具有和内建类型一样的 运算,eg: (a=b)=c
1