简体   繁体   中英

In C++, where is std::cin object defined?

在C ++中,istream对象std::cin在头文件<iostream>声明,但它在哪里定义?

Where it is defined is not mandated by the standard, it's completely up to the implementation.

Here's where mine is:

$ nm -C /usr/local/lib64/libstdc++.so.6 | grep -w cin
0000000000305900 B std::cin

The implementation you're using provides them in some form. The specifics as to where they are at runtime is implementation-defined, but access to them is standard-defined. Their lifetimes and behavior therein are likewise defined by the standard:

C++11 § 27.4.1(2)

The objects are constructed and the associations are established at some time prior to or during the first time an object of class ios_base::Init is constructed, and in any case before the body of main begins execution. 294 The objects are not destroyed during program execution. 295 The results of including <iostream> in a translation unit shall be as if <iostream> defined an instance of ios_base::Init with static storage duration. Similarly, the entire program shall behave as if there were at least one instance of ios_base::Init with static storage duration.

在由编译器驱动程序自动链接到可执行文件的库中(例如g++clang++ )。

Its a Object of class istream . So when you use cin you just create a istream type object. It has external linkage and static duration.

The standard input stream is a source of characters determined by the environment. It is generally assumed to be input from an external source, such as the keyboard or a file .

As an object of class istream , characters can be retrieved either as formatted data using the extraction operator ( operator>> ) or as unformatted data, using member functions such as read .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM