destructor and namespace with vs6 sp0/5 and !g++

D

demo

using visual studio 6 with service pack 0 and 5, why does the distructor of
a class contained inside a namespace isn't called and instead it's called
when:
- iostream.h is included instead of iostream
- "using namespace std;" is commented
?

the destructor of a2 (not contained inside the namespace) is always called.

this problem seems to afflict only visual studio 6 service pack 0 and 5 and
under linux g++ it works fine.

note that the constructor of both a1 and a2 are called and the problem
persist even making a1 a global variable (removing it from the namespace c).

//----------
#include <iostream>
using namespace std;

class a{
public:
a(){cout << "a()" << endl;}
~a(){cout << "a~()" << endl;}
};

namespace c{
a a1;
}

int main(){
a a2;
return 0;
}
//EOF

output:
a()
a()
a~()

bye, demo
 
S

Sharad Kala

demo said:
using visual studio 6 with service pack 0 and 5, why does the distructor of
a class contained inside a namespace isn't called and instead it's called
when:
- iostream.h is included instead of iostream
- "using namespace std;" is commented
?

Problem with VC 6, but then you are using a compiler that is about 8 years
old. If possible get VC 7.x which get this example right.

Sharad
 
M

msalters

demo said:
using visual studio 6 with service pack 0 and 5, why does the distructor of
a class contained inside a namespace isn't called and instead it's called
when:
- iostream.h is included instead of iostream
- "using namespace std;" is commented
?

[ snip example trying to see if a dtor is called by inserting a cout ]

You're using the std::cout object. This should be destroyed fairly
late in the shutdown process, but VC6 destroys it early. That doesn't
mean your dtor isn't called. It is called, it can't produce output.

iostream.h is an older microsoft header, not on-topic here, and I've
got no idea when or how it's ::cout object is destroyed.
Regards,
Michiel Salters
 
M

Mike Hewson

msalters said:
iostream.h is an older microsoft header, not on-topic here,

you can use .h headers if you want to, they're just deprecated. :)

[OT] Anyway VC++ has a whiz bang interface but the compiler sucks.
 
M

Mike Hewson

Sharad said:
Mike Hewson said:
[OT] Anyway VC++ has a whiz bang interface but the compiler sucks.


It's quite good after after v7.1.

Oooo...

I've got VC++ 6 sp5, BUT I've slid in Comeau's compiler 4.3.3 as a
'tool' - so it compiles with Comeau ( compliance!! ) then hands over C
code to the native MS compiler ( linking etc from there on ). Some name
mangling issues for debugging, though not terrible. I'm aiming for a
spread of compilers in similiar vein - Digital Mars, Bloodshed, Mingw
etc. If any one wants to know the skinny on how to do that email me.

Oh... and ... ( I write this attempting to cheekily avoid OT snipes :) )

*COMPLIANCE IS IMPORTANT*
 
O

Old Wolf

msalters said:
you can use .h headers if you want to, they're just deprecated. :)

Only the C headers have .h forms. There never is and never was
an iostream.h, fstream.h, etc.
[OT] Anyway VC++ has a whiz bang interface but the compiler sucks.
This has been addressed in later versions, from what I've heard
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top