problem with the destructor of cout.

B

blackswift

Hello,all

I hava a problem that
when did cout call its destructor?

I used GCC compiler under linux , It compiles OK.
and gives me :
cons
des
as I haved expected.

But I use Visual C++ 6.0(I know it is far from standard..)
It will not give correct answer.
(cout's destructor is called before a's)

I want to know how STANDARD acts in this sitution.

thanks in advance.


#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "cons " << endl;
}
~A() {
cout << "des " << endl;
}
};

A a;

int main()
{

return 0;
}
 
R

Rolf Magnus

blackswift said:
Hello,all

I hava a problem that
when did cout call its destructor?

Never. According to the C++ standard, cout is never destroyed at all.
 
K

Kai-Uwe Bux

blackswift said:
Hello,all

I hava a problem that
when did cout call its destructor?
Never.

I used GCC compiler under linux , It compiles OK.
and gives me :
cons
des
as I haved expected.

But I use Visual C++ 6.0(I know it is far from standard..)
It will not give correct answer.
(cout's destructor is called before a's)

Shouldn't.
I want to know how STANDARD acts in this sitution.

thanks in advance.


#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "cons " << endl;
}
~A() {
cout << "des " << endl;
}
};

A a;

int main()
{

return 0;
}

From the standard [27.3]:

Header <iostream> synopsis

namespace std {
extern istream cin;
extern ostream cout;
extern ostream cerr;
extern ostream clog;
extern wistream wcin;
extern wostream wcout;
extern wostream wcerr;
extern wostream wclog;
}

1 The header <iostream> declares objects that associate objects with the
standard C streams provided for by the functions declared in <cstdio>
(27.8.2).

2 Mixing operations on corresponding wide- and narrow-character streams
follows the same semantics as mixing such operations on FILEs, as
specified in Amendment 1 of the ISO C standard. 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.264) The
objects are not destroyed during program execution.265)

Note the very last line: cout is *not* destroyed.

The footnote 265) clarifies:

265) Constructors and destructors for static objects can access these
objects to read input from stdin or write output to stdout or stderr.



Best

Kai-Uwe Bux
 
B

blackswift

thanks a lot.

but how about:


ofstream cout("myfile.txt");
A a;

Does this cout still not destory during the execution.
 
R

Rolf Magnus

blackswift said:
thanks a lot.

but how about:


ofstream cout("myfile.txt");
A a;

Does this cout still not destory during the execution.

If you define an own object, it will of course be destroyed regardless of
the name. Only the cout that is part of the standard library won't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top