By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 IT Pros & Developers. It's quick & easy.

problem with the destructor of cout.

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.