Reputation: 479
// my first program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
Is cout
an object?
If so, where is it instantiated? (I don't see something like "new ....
")
Upvotes: 5
Views: 322
Reputation: 7180
The current C++ standard states (27.3/2):
[...]The objects are constructed, and the associations are established at some time prior to or during first time an object of class
ios_base::Init
is constructed, and in any case before the body of main begins execution. The objects are not destroyed during program execution.
And from ([iostream.objects]/2:
If a translation unit includes
<iostream>
or explicitly constructs anios_base::Init
object, these stream objects shall be constructed before dynamic initialization of non-local objects defined later in that translation unit."
In C++ parlance a translation-unit is nothing but a compiler terminology for a file and any/all headers which are included into that file.
Upvotes: 2
Reputation: 2665
Yes, cout
is an object. It's instantiated in <iostream>
header file behind your back (together with some other streaming objects like cin
or cerr
) :)
Upvotes: 1
Reputation: 72469
cout
is an object. It's instantiated by the implementation during the startup of your program. That means that it can happen in the CRT DLL or in the code linked statically.
Upvotes: 1
Reputation: 1865
Yes, it is initialize by C++ runtime library when your program startup.
Upvotes: 1
Reputation: 829
Cout is part of the library you just instantiated in the header IOSTREAM.
Upvotes: 0
Reputation: 36852
cout is a global object declared somewhere in <iostream>.
By the way, unlike in Java or C#, you don't need new
to create an object. For instance, this will work:
std::string str; // creates a new std::string object called "str"
Upvotes: 7