2015-01-22 5 views
-3

Это sesms в C++, это хорошо, чтобы повторно назначить ссылку, привязывая ее к другому объекту. Однако приведенный ниже код не подлежит подбору:Почему нельзя переназначить ссылку на istream/ostream?

ostream &os_ref = cerr; 
os_ref = cout; 

Может кто-нибудь сказать мне, что не так? Информация об ошибках приведена ниже.

temp.cpp: In function ‘int main()’: 
temp.cpp:15:12: error: use of deleted function ‘std::basic_ostream<char>& std::basic_ostream<char>::operator=(const std::basic_ostream<char>&)’ 
    os_ref = cout; 
      ^
In file included from /usr/include/c++/4.9/iostream:39:0, 
       from temp.cpp:1: 
/usr/include/c++/4.9/ostream:58:11: note: ‘std::basic_ostream<char>& std::basic_ostream<char>::operator=(const std::basic_ostream<char>&)’ is implicitly deleted because the default definition would be ill-formed: 
    class basic_ostream : virtual public basic_ios<_CharT, _Traits> 
     ^
/usr/include/c++/4.9/ostream:58:11: error: use of deleted function ‘std::basic_ios<char>& std::basic_ios<char>::operator=(const std::basic_ios<char>&)’ 
In file included from /usr/include/c++/4.9/ios:44:0, 
       from /usr/include/c++/4.9/ostream:38, 
       from /usr/include/c++/4.9/iostream:39, 
       from temp.cpp:1: 
/usr/include/c++/4.9/bits/basic_ios.h:66:11: note: ‘std::basic_ios<char>& std::basic_ios<char>::operator=(const std::basic_ios<char>&)’ is implicitly deleted because the default definition would be ill-formed: 
    class basic_ios : public ios_base 
     ^
In file included from /usr/include/c++/4.9/ios:42:0, 
       from /usr/include/c++/4.9/ostream:38, 
       from /usr/include/c++/4.9/iostream:39, 
       from temp.cpp:1: 
/usr/include/c++/4.9/bits/ios_base.h:789:5: error: ‘std::ios_base& std::ios_base::operator=(const std::ios_base&)’ is private 
    operator=(const ios_base&); 
    ^
In file included from /usr/include/c++/4.9/ios:44:0, 
       from /usr/include/c++/4.9/ostream:38, 
       from /usr/include/c++/4.9/iostream:39, 
       from temp.cpp:1: 
/usr/include/c++/4.9/bits/basic_ios.h:66:11: error: within this context 
    class basic_ios : public ios_base 
     ^
+1

«В C++ это хорошо, чтобы повторно присвоить ссылку, ограничивающую его к другому объекту.» ** ** НЕПРАВИЛЬНО. – juanchopanza

+0

Ответ должен быть здесь: http://stackoverflow.com/questions/8070537/how-should-i-correctly-assign-cout-to-a-static-ostream-reference-variable?rq=1 – stefan

+0

Нет, это здесь: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list –

ответ

3

В C++ все в порядке, чтобы повторно назначить ссылку, привязывая ее к другому объекту.

Нет, это не

Может кто-нибудь сказать мне, что случилось?

В C++ вы не можете повторно назначить ссылку для привязки к другому объекту. Ссылка является псевдонимом другого объекта. Если вы «назначаете» ссылку, вы назначаете упомянутый объект, не изменяя объект, на который ссылается ссылка.

Например:

flag 

int a = 3, b = 4; 
int &ref = a; 
ref = b;      // like saying a = b; 
std::cout << a << std::endl; // prints 4 
+0

Извините, я думал, что это так. Но почему может компилироваться следующий код? – Warbean

+0

int a = 3, b = 4; int & ref = a; ref = b; – Warbean

+0

oops Я понимаю сейчас ... – Warbean

3

Ссылки привязаны к объекту, вы никогда не сможете переназначить его.

Ссылка остается как псевдоним, к которому он был инициализирован во время создания.

Смежные вопросы