Q
Qin Chen
I will present very long code, hope someone will read it all, and teach me
something like tom_usenet.
This question comes to me when i read <<Think in C++>> 2nd, chapter 10 ,
name control, section "Static initialization dependency". There is a
example to show how to solve the prolem involved with a technique first
poineered by Jerry Schwarz while creating the iostream library (because the
definitions for cin, cout, and cerr are static and live in a separate file).
The idea is to use a additional class responsible for the dynamic
initialization of your library¡¯s static objects.
Here in the code following, the static objects is std:fstream out,
intended to be used as a log file maybe, and the addition class is
Initializer. In class Initializer's constructor, out initialize the log
file... but code stop there with a access voilation exception.......
I'm eager to know why....
----------------------------------------------------------------------------
----
//: C10:Initializer.h
// Static initialization technique
#ifndef INITIALIZER_H
#define INITIALIZER_H
#include <iostream>
#include <fstream>
extern std:fstream out;
class Initializer {
static int initCount;
public:
Initializer();
~Initializer();
};
// The following creates one object in each
// file where Initializer.h is included, but that
// object is only visible within that file:
static Initializer init;
#endif // INITIALIZER_H ///:~
-----------------------------------------------------------------------
//Initializer.cpp
#include "Initializer.h"
#include <fstream>
#include <iostream>
Initializer::Initializer(){
// Initialize first time only
if(initCount++ == 0) {
std::cout << out.is_open() << std::endl; //return 0
// to test if out is good to work with
///////////////////////////////////////////////////////////////////
out.open("hello.txt"); // program stuck here......
///////////////////////////////////////////////////////////////////
// debug give infomation like this :
//First-chance exception in test.exe: 0xC0000005: Access Violation.
// i use VC++ 6.0.
}
}
Initializer::~Initializer()
{
std::cout << "~Initializer()" << std::endl;
// Clean up last time only
if(--initCount == 0) {
out.close();
// Any necessary cleanup here
}
}
------------------------------------------------------------------------
//: C10:InitializerDefs.cpp {O}
// Definitions for Initializer.h
#include "Initializer.h"
#include <fstream>
// Static initialization will force
// all these values to zero:
std:fstream out;
int Initializer::initCount;
///:~
------------------------------------------------------------------------
// test file......
//: C10:Initializer2.cpp
//{L} InitializerDefs Initializer
// Static initialization
#include "Initializer.h"
using namespace std;
int main() {
out << "hello" << endl;
return 0;
} ///:~
------------------------------------------------------------------------
something like tom_usenet.
This question comes to me when i read <<Think in C++>> 2nd, chapter 10 ,
name control, section "Static initialization dependency". There is a
example to show how to solve the prolem involved with a technique first
poineered by Jerry Schwarz while creating the iostream library (because the
definitions for cin, cout, and cerr are static and live in a separate file).
The idea is to use a additional class responsible for the dynamic
initialization of your library¡¯s static objects.
Here in the code following, the static objects is std:fstream out,
intended to be used as a log file maybe, and the addition class is
Initializer. In class Initializer's constructor, out initialize the log
file... but code stop there with a access voilation exception.......
I'm eager to know why....
----------------------------------------------------------------------------
----
//: C10:Initializer.h
// Static initialization technique
#ifndef INITIALIZER_H
#define INITIALIZER_H
#include <iostream>
#include <fstream>
extern std:fstream out;
class Initializer {
static int initCount;
public:
Initializer();
~Initializer();
};
// The following creates one object in each
// file where Initializer.h is included, but that
// object is only visible within that file:
static Initializer init;
#endif // INITIALIZER_H ///:~
-----------------------------------------------------------------------
//Initializer.cpp
#include "Initializer.h"
#include <fstream>
#include <iostream>
Initializer::Initializer(){
// Initialize first time only
if(initCount++ == 0) {
std::cout << out.is_open() << std::endl; //return 0
// to test if out is good to work with
///////////////////////////////////////////////////////////////////
out.open("hello.txt"); // program stuck here......
///////////////////////////////////////////////////////////////////
// debug give infomation like this :
//First-chance exception in test.exe: 0xC0000005: Access Violation.
// i use VC++ 6.0.
}
}
Initializer::~Initializer()
{
std::cout << "~Initializer()" << std::endl;
// Clean up last time only
if(--initCount == 0) {
out.close();
// Any necessary cleanup here
}
}
------------------------------------------------------------------------
//: C10:InitializerDefs.cpp {O}
// Definitions for Initializer.h
#include "Initializer.h"
#include <fstream>
// Static initialization will force
// all these values to zero:
std:fstream out;
int Initializer::initCount;
///:~
------------------------------------------------------------------------
// test file......
//: C10:Initializer2.cpp
//{L} InitializerDefs Initializer
// Static initialization
#include "Initializer.h"
using namespace std;
int main() {
out << "hello" << endl;
return 0;
} ///:~
------------------------------------------------------------------------