static initialization order fiasco

A

anon

When I was checking this:
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12
(questions until 10.16)


This code:

//////////////////////////////////////////////////
#include <iostream>
#include <memory>

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

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

~b() { std::cout<<"~b()"<<std::endl; }
};
class c
{
public:
static a& k()
{
static std::auto_ptr< a > p( new a );
return *p;
}
static b& j()
{
static b* p( new b );
return *p;
}
};
int main()
{
c::k();
c::j();
}
//////////////////////////////////////////////////

the output is:
a()
b()
~a()


So, the question is why the faq doesn't suggest this solution (with
auto_ptr) to the static initialization order fiasco?
 

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
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top