2017-10-25 14 views
1
#include <iostream> 
#include <string> 
#include <vector> 

class X 
{ 
    public: 
    int& a; 
    int&& b; 

    X(int& c, int && d):a(c), b(std::move(d)) 
    { 

    } 
}; 

X* x = nullptr; 
void fun() 
{ 

    std::cout<<"Fun\n"; 
    int l = 8; 
    int r = 9; 
    std::cout << &(l) << std::endl; 
    std::cout << &(r) << std::endl; 
    x = new X(l, std::move(r));  
} 

int main() 
{ 
    fun(); 

    std::cout << x->a << std::endl; 
    std::cout << &(x->a) << std::endl; 

    std::cout << x->b << std::endl; 
    std::cout << &(x->b) << std::endl; 

} 

=> 회원 변수 참조 값 (lvaluervalue)은 쓰레기가됩니까?
다른 컴파일러에서 다른 동작이 나타납니다. 그래서 C++ 표준이 이것에 대해 뭐라고하는지 알고 싶었습니다.클래스의 멤버 변수로 Rvalue 및 Lvalue 참조 - 유효합니까?

+2

'rvalue' * 참조 * 데이터 멤버로 * 잘 끝내지 않을 것입니다 .... BTW, 당신은 [전체 타입을 실제로 움직일 수 없습니다.] (https://stackoverflow.com/questions/14679605/do-built-types-have-move-semantics) .. – WhiZTiM

답변

1

로컬 변수에 대한 참조 멤버를 바인딩하고 있는데, 이는 fun() 함수에서 벗어날 때 소멸됩니다. 그 후, 두 참조 모두 댕글 거리게되고, 그것들에 대한 참조는 UB로 이어진다.

lvalue 및 rvalue 참조 멤버 모두에 해당됩니다.