2010-02-09 129 views
5

我被告知引用变量必须在初始化列表中初始化,但为什么这是错误的?在初始化列表中初始化引用

class Foo 
    { 
    public: 
     Foo():x(0) {  
     y = 1; 
     } 
    private: 
     int& x; 
     int y; 
    }; 

因为0是一个临时对象吗?如果是这样,什么样的对象可以被引用绑定?可以接收地址的对象?

谢谢!

回答

15

0不是左值,它是右值。你不能修改它,但你试图绑定到一个可以修改的引用。

如果您提供参考const,它将按预期工作。考虑一下:

int& x = 0; 
x = 1; // wtf :(

这显然是一个不行。但const&“可以绑定到临时量(右值):

const int& x = 0; 
x = 1; // protected :) [won't compile] 

注意的是,临时的生活时间是在构造函数中完成结束。如果你把静态存储你的不变,你将会很安全:

class Foo 
{ 
public: 
    static const int Zero = 0; 

    Foo() : x(Zero) // Zero has storage 
    { 
     y = 1; 
    } 
private: 
    const int& x; 
    int y; 
}; 
+0

虽然构造函数中的具体示例如果声明了成员变量'const',将会失败。 – Omnifarious

+1

@Omnifarious:这是怎么回事? – GManNickG

+0

临时0被破坏时会发生什么?如果它的地址被提取并且被解除引用,那么如何? – Omnifarious

0

长住引用必须绑定到一个lvalue。基本上,正如你雄辩地说,一个具有明确地址的对象。如果它们被绑定到一个临时的临时将被销毁,而引用仍然引用它,结果是未定义的。

短暂的const引用(局部函数变量和函数参数)可以绑定到临时对象。如果是,那么临时保证在参考超出范围之前不被销毁。

示范代码:

#include <iostream> 

class Big { 
public: 
    Big() : living_(true), i_(5) { // This initialization of i is strictly legal but 
     void *me = this;   // the result is undefined. 
     ::std::cerr << "Big constructor called for " << me << "\n"; 
    } 
    ~Big() { 
     void *me = this; 
     living_ = false; 
     ::std::cerr << "Big destructor called for " << me << "\n"; 
    } 

    bool isLiving() const { return living_; } 
    const int &getIref() const; 
    const int *getIptr() const; 

private: 
    ::std::string s_; 
    bool living_; 
    const int &i_; 
    char stuff[50]; 
}; 

const int &Big::getIref() const 
{ 
    return i_; 
} 

const int *Big::getIptr() const 
{ 
    return &i_; 
} 

inline ::std::ostream &operator <<(::std::ostream &os, const Big &b) 
{ 
    const void *thisb = &b; 
    return os << "A " << (b.isLiving() ? "living" : "dead (you're lucky this didn't segfault or worse)") 
      << " Big at " << thisb 
      << " && b.getIref() == " << b.getIref() 
      << " && *b.getIptr() == " << *b.getIptr(); 
} 

class A { 
public: 
    A() : big_(Big()) {} 

    const Big &getBig() const { return big_; } 

private: 
    const Big &big_; 
}; 

int main(int argc, char *argv[]) 
{ 
    A a; 
    const Big &b = Big(); 
    const int &i = 0; 
    ::std::cerr << "a.getBig() == " << a.getBig() << "\n"; 
    ::std::cerr << "b == " << b << "\n"; 
    ::std::cerr << "i == " << i << "\n"; 
    return 0; 
} 

和输出:

Big constructor called for 0x7fffebaae420 
Big destructor called for 0x7fffebaae420 
Big constructor called for 0x7fffebaae4a0 
a.getBig() == A living Big at 0x7fffebaae420 && b.getIref() == -341121936 && *b.getIptr() == -341121936 
b == A living Big at 0x7fffebaae4a0 && b.getIref() == 0 && *b.getIptr() == 0 
i == 0 
Big destructor called for 0x7fffebaae4a0 
0

好吧,你永远无法改变它,0可以将其他从未等于什么比0

尝试

class Foo 
    { 
    public: 
     Foo(int& a):x(a) {  
     y = 1; 
     } 
    private: 
     int& x; 
     int y; 
    }; 

Alte实际上,如果你的引用是恒定的,你可以这样做,因为0只能等于零

+0

构造函数完成后,该引用将变为无效。 :/ – GManNickG

+0

不,当传入构造函数的int超出调用函数的范围时,引用变为无效 – Steve

+0

是的,后编辑响应非常低。你应该提到“我修好了”。不是,“你错了。” :( – GManNickG