I am trying to tie these two classes into my Network classes. Presumably, you should only call WSAStartup/WSACleanup in non-overlapping pairs, so I devised this as a way of transparently handling that problem. The compiler errors are simply undefined references to variables of the "static" class. How can I fix that?
Code:
//...this class should only be created by "new", and destroyed with "delete"...NO LOCALS...
class WSAInitializer {
private:
WSAData wsaData;
bool awaiting_shutdown;
public:
WSAInitializer() {
awaiting_shutdown = false;
if(WSAStartup(MAKEWORD(1, 1), &wsaData) == 0) {
awaiting_shutdown = true;
}
}
~WSAInitializer() {
if(awaiting_shutdown) {
WSACleanup();
}
}
};
//....
//...this should be the only class using a WSAInitializer:: object...
//...also, balanced Startup()/Cleanup() calls will ensure ref_count's integrity...(use in constructor/destructor pairs).
class WSA {
private:
static WSAInitializer * instance;
static int ref_count;
public:
WSA() {
Announce("Initializing...");
WSA::ref_count = 0;
}
static void Startup() {
++WSA::ref_count;
if(WSA::ref_count == 1) {
WSA::instance = new WSAInitializer;
Announce("WSAStartup Initiated...");
}
}
static void Cleanup() {
--WSA::ref_count;
if(WSA::ref_count == 0) {
delete WSA::instance;
Announce("WSACleanup Initiated...");
}
}
}wsa; //...added this to quiet the compiler, but no avail.
//...EXAMPLE:
class Foo {
public:
Foo() {
WSA::Startup();
}
~Foo() {
WSA::Cleanup();
}
};
int main() {
if(condition) {
Foo bar; //...new WSAInitializer:: born
if(condition) {
Foo blat;
} //...blat's destructor called, WSA::ref_count decrements....
} //...bar's destructor called, WSA::destroys current WSAInitializer...
return 0;
}