468,765 Members | 1,493 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

problem with the destructor of cout.

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;
}

Apr 14 '06 #1
4 1944
blackswift wrote:
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.

Apr 14 '06 #2
blackswift wrote:
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
Apr 14 '06 #3
thanks a lot.

but how about:
ofstream cout("myfile.txt");
A a;

Does this cout still not destory during the execution.

Apr 14 '06 #4
blackswift wrote:
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.

Apr 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Stub | last post: by
11 posts views Thread by Andrew Skouloudis | last post: by
7 posts views Thread by Emanuel Ziegler | last post: by
5 posts views Thread by August1 | last post: by
3 posts views Thread by rahul8143 | last post: by
7 posts views Thread by dragoncoder | last post: by
8 posts views Thread by lmfmaw | last post: by
3 posts views Thread by GAURAV AGRAWAL | last post: by
6 posts views Thread by asm23 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.