Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
May 04, 2004 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 Re: Singleton | ||||
---|---|---|---|---|
| ||||
Posted in reply to Evain Jb | You should create your Singleton before using it.
Evain Jb wrote:
> 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 Re: Singleton | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stephan Wienczny | In article <c78ocd$109d$1@digitaldaemon.com>, Stephan Wienczny says... >You should create your Singleton before using it. The primary goal of the Singleton is not to have to create it before using it. The field m_inst, the instance of the Singleton, is created at the first access of the static accessor inst() By the way, to prevent the Singleton to be created, the constructor is private. That is why I'm wondering on the position of the access violation. |
May 04, 2004 Re: Singleton | ||||
---|---|---|---|---|
| ||||
Posted in reply to Evain Jb |
> public:
> static Singleton inst() {
> if (m_inst == null) {
> m_inst = new Singleton();
> }
> return m_inst;
> }
I think the == should be "is" or "==="
|
May 04, 2004 Re: Singleton | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | 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... > > > public: > > static Singleton inst() { > > if (m_inst == null) { > > m_inst = new Singleton(); > > } > > return m_inst; > > } > > I think the == should be "is" or "===" > > |
Copyright © 1999-2021 by the D Language Foundation