|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
D - Singleton
This snippet compiles well :
import std.c.stdio;
class Singleton {
private:
static this() {
m_inst = null;
}
static Singleton m_inst = null;
char[] m_sentence;
this() {
m_sentence = "Hello Singleton";
}
public:
static Singleton inst() {
if (m_inst == null) {
m_inst = new Singleton();
}
return m_inst;
}
char[] sentence() {
return m_sentence;
}
}
int main(char[][] args) {
assert(Singleton.inst != null);
puts(Singleton.inst.sentence);
return 1;
}
But when I try to execute it, here is the output :
D:\software\dide\Projects\Proto>proto
Error: Access Violation
And using
Digital Mars D Compiler v0.86
Do I miss something ?
May 04 2004
You should create your Singleton before using it. Evain Jb wrote: May 04 2004
In article <c78ocd$109d$1 digitaldaemon.com>, Stephan Wienczny says...You should create your Singleton before using it. May 04 2004
May 04 2004
Yeah; that syntax seems to be a common issue, and it takes a little while for it to become second nature. Use (x is null) or (x === null) Conversely, use (x !== null) or !(x is null) or just (x) I suppose it might be nice to have a corresponding "not" keyword (to match "is") but ... Also, when testing arrays handles the codegen is better if you are explicit about the null test, rather than doing the simple (x) test. The reason being that array handles are 64bits wide rather than 32 (includes the length). FWIW, I've taken to testing the length of an array since that's valid whether the pointer is or not. To me it just looks a little cleaner ... - Kris "Ben Hinkle" <bhinkle4 juno.com> wrote in message news:c78si2$175t$1 digitaldaemon.com... May 04 2004
(I'm mostly cutting and pasting from the last time I wrote this.) Just so you know... This is the "old" newsgroup. Please post new threads in the new newsgroup. *GENERAL D Discussions* Web: http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D Newsgroup: news://news.digitalmars.com/digitalmars.D *BUG REPORTS for DMD* Web: http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.bugs Newsgroup: news://news.digitalmars.com/digitalmars.D.bugs *D GNU newsgroup* Web: http://www.digitalmars.com/drn-bin/wwwnews?D.gnu Newsgroup: news://news.digitalmars.com/D.gnu *Old D newsgroup* (Please don't post new threads here!) Web: http://www.digitalmars.com/drn-bin/wwwnews?D Newsgroup: news://news.digitalmars.com/D Unofficial forums... *dsource.org Forums* (phpBB) http://www.dsource.org/forums/ It's also good courtesy to examine the Frequently Asked Questions before posting: http://www.digitalmars.com/d/faq.html http://www.wikiservice.at/d/wiki.cgi?FaqRoadmap -- Justin http://jcc_7.tripod.com/d/ May 04 2004
|