why is cerr used here and not cout

So I was with Stroustrup's book PPP chapter 18 (link to chapter here)
http://www.informit.com/articles/article.aspx?p=2216986&seqNum=4

and on the last two codes that I put down below on this same thread in case you prefer going straight to the code, I was a bit confused by how my console was outputting things on the screen without a cout, I presume now it is using cerr instead which I though was only for error messages

also: if i just put these two includes I get an error at the first "<<", it is only when I use the std lib header from the book that I get no errors

and in the end: why am i getting on the console results (written below code here) such random numbers? I though I was gonna get hex numbers as in starting with 0x etc... to be honest most of what I am getting I don't really see where it comes from



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  #include <iostream>
#include <vector>
using namespace std;
 
struct X { // simple test class
int val;
void out(const string& s, int nv)
{ cerr << this << "–>" << s << ": " << val << " (" << nv << ")\n"; }
X(){ out("X()",0); val=0; } // default constructor
X(int v) { val=v; out( "X(int)",v); }X(const X& x){ val=x.val; out("X(X&) ",x.val); } // copy constructor
X& operator=(const X& a) // copy assignment
{ out("X::operator=()",a.val); val=a.val; return *this; }
~X() { out("~X()",0); } // destructor
};
 
 
X glob(2); // a global variable
X copy(X a) { return a; }
X copy2(X a) { X aa = a; return aa; }
X& ref_to(X& a) { return a; }
X* make(int i) { X a(i); return new X(a); }
struct XX { X a; X b; };
int main()
{
X loc {4}; // local variable
X loc2 {loc}; // copy construction
loc = X{5}; // copy assignment
loc2 = copy(loc); // call by value and return
loc2 = copy2(loc);
X loc3 {6};
X& r = ref_to(loc); // call by reference and return
delete make(7);
delete make(8);
vector<X> v(4); // default values
XX loc4;
X* p = new X{9}; // an X on the free store
delete p;
X* pp = new X[5]; // an array of Xs on the free store
delete[] pp;
}




Here my console result:

002352E0û>X(int): 2 (2)
0037FA14û>X(int): 4 (4)
0037FA08û>X(X&) : 4 (4)
0037F8E4û>X(int): 5 (5)
0037FA14û>X::operator=(): 4 (5)
0037F8E4û>~X(): 5 (0)
0037F800û>X(X&) : 5 (5)
0037F8CCû>X(X&) : 5 (5)
0037F800û>~X(): 5 (0)
0037FA08û>X::operator=(): 4 (5)
0037F8CCû>~X(): 5 (0)
0037F800û>X(X&) : 5 (5)
0037F7DCû>X(X&) : 5 (5)
0037F8B4û>X(X&) : 5 (5)
0037F7DCû>~X(): 5 (0)
0037F800û>~X(): 5 (0)
0037FA08û>X::operator=(): 5 (5)
0037F8B4û>~X(): 5 (0)
0037F9FCû>X(int): 6 (6)
0037F7E0û>X(int): 7 (7)
0074FEF0û>X(X&) : 7 (7)
0037F7E0û>~X(): 7 (0)
0074FEF0û>~X(): 7 (0)
0037F7E0û>X(int): 8 (8)
0074FF50û>X(X&) : 8 (8)
0037F7E0û>~X(): 8 (0)
0074FF50û>~X(): 8 (0)
0074A690û>X(): -842150451 (0)
0074A694û>X(): -842150451 (0)
0074A698û>X(): -842150451 (0)
0074A69Cû>X(): -842150451 (0)
0037F9C8û>X(): -858993460 (0)
0037F9CCû>X(): -858993460 (0)
0074FE60û>X(int): 9 (9)
0074FE60û>~X(): 9 (0)
007520ACû>X(): -842150451 (0)
007520B0û>X(): -842150451 (0)
007520B4û>X(): -842150451 (0)
007520B8û>X(): -842150451 (0)
007520BCû>X(): -842150451 (0)
007520BCû>~X(): 0 (0)
007520B8û>~X(): 0 (0)
007520B4û>~X(): 0 (0)
007520B0û>~X(): 0 (0)
007520ACû>~X(): 0 (0)
0037F9CCû>~X(): 0 (0)
0037F9C8û>~X(): 0 (0)
0074A690û>~X(): 0 (0)
0074A694û>~X(): 0 (0)
0074A698û>~X(): 0 (0)
0074A69Cû>~X(): 0 (0)
0037F9FCû>~X(): 6 (0)
0037FA08û>~X(): 5 (0)
0037FA14û>~X(): 5 (0)
002352E0û>~X(): 2 (0)
Presione una tecla para continuar . . .
its supposedly a test that leaves print of constructors and destructors as to be able to understand them better presumably, I am however always having problems with this books code on Visual Studio Community 2015 as it seems fairly obsolete perhaps. I understand that from the point of view of functionality this sucks but that didnt really clear any of my doubts
For the number format, try doing std::cerr << std::hex << ...
I was a bit confused by how my console was outputting things on the screen without a cout, I presume now it is using cerr instead which I though was only for error messages

Yes by default both cout and cerr print to the console. However both of these streams can be redirected to other locations by various methods such as input and output redirection. There are subtle differences between these two streams, cout is by default buffered meaning the data will not be written to the stream until the buffer fills or you manually flush the buffer, cerr is by default un-buffered which means that the data is written immediately to the stream.


Topic archived. No new replies allowed.