2011-10-10 14 views

回答

61

这是不可能的,那就是by design。参考资料不能被反弹。

+0

在下面的代码中,我将x的引用更改为k并仍然得到结果。你能告诉我这有可能吗? int a = 50; int &x=a; int k = 10; cout << x << endl; x = k; cout << x << endl; – Rajesh

+1

您并未更改参考,您正在将'k'的值分配给'x'所指的对象。在这个赋值之后,'a == k'和'x'仍然指向'a'。 –

7

您不能重新分配参考。

16

你不能重新分配引用,但是如果你正在寻找能够提供类似功能的东西,你可以改为使用指针。

int a = 2; 
int b = 4; 
int* ptr = &a; //ptr points to memory location of a. 
ptr = &b;  //ptr points to memory location of b now. 

你可以得到或指针内设置的值: 

*ptr = 5;  //set 
int c = *ptr; //get 
+3

您正在重新声明'ptr'。 –

7

这是不可能的,你想要的方式。 C++不会让你重新引用参考指向的内容。

但是,如果你要使用欺骗,你几乎可以以新的范畴(NEVER在实际的程序中做到这一点),模拟它:

int a = 2; 
int b = 4; 
int &ref = a; 

{ 
    int& ref = b; // Shadows the original ref so everything inside this { } refers to `ref` as `b` now. 
} 
+4

几乎-1值得 – ThomasMcLeod

48

用C++ 11有新的(ISH )std::reference_wrapper

#include <functional> 

int main() { 
    int a = 2; 
    int b = 4; 
    auto ref = std::ref(a); 
    //std::reference_wrapper<int> ref = std::ref(a); <- Or with the type specified 
    ref = std::ref(b); 
} 

这对于在容器中存储引用也很有用。

0

虽然它的,因为它违背了使用引用的目的,一个坏主意,有可能使用放置新更改参考直接

const_cast< int& >(ref)=b; 
+7

人们+1错误的解决方案很有趣。这段代码将会改变ref引用的地方,而不是引用自身 – Slava

+0

在Exchange站点上我再也没有看到:D – GameDeveloper

+0

嘿,这显然是一个错误的答案。 @visu,请删除它。 – einpoklum

3

你可以做一个参考的包装非常简单:

template< class T > 
class RefWrapper 
{ 
public: 
    RefWrapper(T& v) : m_v(v){} 

    operator T&(){ return m_v; } 
    T& operator=(const T& a){ m_v = a; return m_v;} 
    //...... // 
    void remap(T& v) 
    { 
     //re-map reference 
     new (this) RefWrapper(v); 
    } 

private: 
    T& m_v; 
}; 


int32 a = 0; 
int32 b = 0; 
RefWrapper<int> r(a); 

r = 1; // a = 1 now 
r.remap(b); 
r = 2; // b = 2 now 
+2

这个答案比必要的复杂得多 - 问题非常简单,可以用一行代码来回答。 – lukelazarovic

3

这是可能的。因为在引擎盖下,引用是一个指针。 下面的代码将打印的“Hello World”

#include "stdlib.h" 
#include "stdio.h" 
#include <string> 

using namespace std; 

class ReferenceChange 
{ 
public: 
    size_t otherVariable; 
    string& ref; 

    ReferenceChange() : ref(*((string*)NULL)) {} 

    void setRef(string& str) { 
     *(&this->otherVariable + 1) = (size_t)&str; 
    } 
}; 

void main() 
{ 
    string a("hello"); 
    string b("world"); 

    ReferenceChange rc; 

    rc.setRef(a); 
    printf("%s ", rc.ref.c_str()); 

    rc.setRef(b); 
    printf("%s\n", rc.ref.c_str()); 
} 
+0

无论你对UB有什么影响并不意味着它“有效” – Slava

+0

@Slava:它实际上是不确定的行为吗?没有把参考定义为“伪装指针”,可以这么说吗? – einpoklum

+0

'setVeriable'不需要在'setRef'中使用'*((uintptr_t *)this)=((uintptr_t)&str)'' – joas

4

从形式上来讲,这是不可能的,因为它被设计禁止。任意说,这是可能的。

引用存储为指针,所以只要知道如何获取地址,就可以随时更改它指向的位置。同样,如果您无权访问,也可以更改常量变量,常量成员变量甚至私有成员变量的值。

例如,下面的代码已经改变了A级的常量私有成员参考:

#include <iostream> 
using namespace std; 

class A{ 
private: 
    const int &i1; 
public: 
    A(int &a):i1(a){} 
    int geti(){return i1;} 
    int *getip(){return (int*)&i1;} 
}; 

int main(int argc, char *argv[]){ 
    int i=5, j=10; 
    A a(i); 
    cout << "before change:" << endl; 
    cout << "&a.i1=" << a.getip() << " &i=" << &i << " &j="<< &j << endl; 
    cout << "i=" << i << " j=" <<j<< " a.i1=" << a.geti() << endl; 
    i=6; cout << "setting i to 6" << endl; 
    cout << "i=" << i << " j=" <<j<< " a.i1=" << a.geti() << endl; 

    *(int**)&a = &j; // the key step that changes A's member reference 

    cout << endl << "after change:" << endl; 
    cout << "&a.i1=" << a.getip() << " &i=" << &i << " &j="<< &j << endl; 
    cout << "i=" << i << " j=" <<j<< " a.i1=" << a.geti() << endl; 
    j=11; cout << "setting j to 11" << endl; 
    cout << "i=" << i << " j=" <<j<< " a.i1=" << a.geti() << endl; 
    return 0; 
} 

程序输出:

before change: 
&a.i1=0x7fff1b624140 &i=0x7fff1b624140 &j=0x7fff1b624150 
i=5 j=10 a.i1=5 
setting i to 6 
i=6 j=10 a.i1=6 

after change: 
&a.i1=0x7fff1b624150 &i=0x7fff1b624140 &j=0x7fff1b624150 
i=6 j=10 a.i1=10 
setting j to 11 
i=6 j=11 a.i1=11 

正如你可以看到,一个。i1最初指向i,更改后指向j

但是,这样做被认为是危险的,因此不予推荐,因为它破坏了数据封装和OOP的原始目的。这更像是内存地址黑客行为。