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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top