Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
July 25, 2004 Need a little help (probably being stupid) | ||||
---|---|---|---|---|
| ||||
I've downloaded the new D compiler to play with. However, I think I'm missing something important which I'm too blind for to see myself? It's about accessing class members, they throw access violation errors on both versions Direct and Indirect, even when Bar is public: // Small access violation example class Foo { public: int Bar; void setBar(int newvalue) { this.Bar = newvalue; printf("%i", this.Bar); } } void main() { Foo spawn; version(Direct) { printf("As though it was public: \n"); spawn.Bar = 2; printf("%i", spawn.Bar); } version(Indirect) { printf("As though it was private: \n"); spawn.setBar(1); } } Could someone point out where I'm being stupid? It's compiled with dmd test.d -version=Direct/Indirect -of test.exe... |
July 25, 2004 Re: Need a little help (probably being stupid) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dn7 | Dn7 wrote: > I've downloaded the new D compiler to play with. However, I think I'm missing > something important which I'm too blind for to see myself? It's about accessing > class members, they throw access violation errors on both versions Direct and > Indirect, even when Bar is public: > > // Small access violation example > class Foo { > public: > int Bar; > void setBar(int newvalue) { > this.Bar = newvalue; > printf("%i", this.Bar); > } > } > void main() { > Foo spawn; ^------------ Foo spawn = new Foo; > version(Direct) { > printf("As though it was public: \n"); > spawn.Bar = 2; > printf("%i", spawn.Bar); > } > version(Indirect) { > printf("As though it was private: \n"); > spawn.setBar(1); > } > } > > Could someone point out where I'm being stupid? It's compiled with dmd test.d > -version=Direct/Indirect -of test.exe... > > |
July 25, 2004 Re: Need a little help (probably being stupid) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dn7 | Dn7 wrote: > I've downloaded the new D compiler to play with. However, I think I'm missing > something important which I'm too blind for to see myself? It's about accessing > class members, they throw access violation errors on both versions Direct and > Indirect, even when Bar is public: > > // Small access violation example > class Foo { > public: > int Bar; > void setBar(int newvalue) { > this.Bar = newvalue; > printf("%i", this.Bar); > } > } > void main() { > Foo spawn; Try this: Foo spawn = new Foo(); More info: http://www.prowiki.org/wiki4d/wiki.cgi?ErrorMessages#Un-initializedObject http://www.prowiki.org/wiki4d/wiki.cgi?ShortFrequentAnswers > version(Direct) { > printf("As though it was public: \n"); > spawn.Bar = 2; > printf("%i", spawn.Bar); > } > version(Indirect) { > printf("As though it was private: \n"); > spawn.setBar(1); > } > } > > Could someone point out where I'm being stupid? It's compiled with dmd test.d > -version=Direct/Indirect -of test.exe... -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/ |
July 25, 2004 Re: Need a little help (probably being stupid) | ||||
---|---|---|---|---|
| ||||
Posted in reply to J C Calvarese | J C Calvarese wrote: > Dn7 wrote: > >> I've downloaded the new D compiler to play with. However, I think I'm missing >> something important which I'm too blind for to see myself? It's about accessing >> class members, they throw access violation errors on both versions Direct and >> Indirect, even when Bar is public: >> >> // Small access violation example >> class Foo { >> public: >> int Bar; >> void setBar(int newvalue) { >> this.Bar = newvalue; >> printf("%i", this.Bar); >> } >> } >> void main() { >> Foo spawn; > > > Try this: > Foo spawn = new Foo(); Quick question, which is the "correct" way? ... = new Foo; or ... = new Foo(); Just wondering because thy both work! > More info: > http://www.prowiki.org/wiki4d/wiki.cgi?ErrorMessages#Un-initializedObject > http://www.prowiki.org/wiki4d/wiki.cgi?ShortFrequentAnswers > >> version(Direct) { >> printf("As though it was public: \n"); >> spawn.Bar = 2; >> printf("%i", spawn.Bar); >> } >> version(Indirect) { >> printf("As though it was private: \n"); >> spawn.setBar(1); >> } >> } >> >> Could someone point out where I'm being stupid? It's compiled with dmd test.d >> -version=Direct/Indirect -of test.exe... > > |
July 25, 2004 Re: Need a little help (probably being stupid) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Edwards | In article <ce0oak$2e4n$1@digitaldaemon.com>, Andrew Edwards says... > >Dn7 wrote: > >> I've downloaded the new D compiler to play with. However, I think I'm missing something important which I'm too blind for to see myself? It's about accessing class members, they throw access violation errors on both versions Direct and Indirect, even when Bar is public: >> >> // Small access violation example >> class Foo { >> public: >> int Bar; >> void setBar(int newvalue) { >> this.Bar = newvalue; >> printf("%i", this.Bar); >> } >> } >> void main() { >> Foo spawn; > ^------------ Foo spawn = new Foo; > >> version(Direct) { >> printf("As though it was public: \n"); >> spawn.Bar = 2; >> printf("%i", spawn.Bar); >> } >> version(Indirect) { >> printf("As though it was private: \n"); >> spawn.setBar(1); >> } >> } >> >> Could someone point out where I'm being stupid? It's compiled with dmd test.d -version=Direct/Indirect -of test.exe... >> >> Thanks :) That's so... JavaScriptish. |
July 25, 2004 Re: Need a little help (probably being stupid) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Edwards | Andrew Edwards wrote: > J C Calvarese wrote: > >> Dn7 wrote: >> >>> I've downloaded the new D compiler to play with. However, I think I'm missing >>> something important which I'm too blind for to see myself? It's about accessing >>> class members, they throw access violation errors on both versions Direct and >>> Indirect, even when Bar is public: >>> >>> // Small access violation example >>> class Foo { >>> public: >>> int Bar; >>> void setBar(int newvalue) { >>> this.Bar = newvalue; >>> printf("%i", this.Bar); >>> } >>> } >>> void main() { >>> Foo spawn; >> >> >> >> Try this: >> Foo spawn = new Foo(); > > > Quick question, which is the "correct" way? > > .... = new Foo; > or > .... = new Foo(); > > Just wondering because thy both work! If it works, I'd say it's right. :) I usually see "Foo()" used, but apparently it doesn't matter if there are parentheses or not if it doesn't have any parameters. > >> More info: >> http://www.prowiki.org/wiki4d/wiki.cgi?ErrorMessages#Un-initializedObject >> http://www.prowiki.org/wiki4d/wiki.cgi?ShortFrequentAnswers >> >>> version(Direct) { >>> printf("As though it was public: \n"); >>> spawn.Bar = 2; >>> printf("%i", spawn.Bar); >>> } >>> version(Indirect) { >>> printf("As though it was private: \n"); >>> spawn.setBar(1); >>> } >>> } >>> >>> Could someone point out where I'm being stupid? It's compiled with dmd test.d >>> -version=Direct/Indirect -of test.exe... -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/ |
July 26, 2004 Re: Need a little help (probably being stupid) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dn7 | Dn7 wrote:
> Thanks :) That's so... JavaScriptish.
It's also C++ish and Javaish. Whereas C++ allows you to create class instances on the stack, D does not. All class instances must be created on the heap, and all class instance variables are references. Structs, on the other hand, may be instanced on the stack. So the following is okay:
struct Foo { int bar; }
Foo f;
f.bar = 1;
|
July 26, 2004 Re: Need a little help (probably being stupid) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Edwards | "Andrew Edwards" <ridimz_at@yahoo.dot.com> wrote in message news:ce0olt$2ebj$1@digitaldaemon.com... > Quick question, which is the "correct" way? > > ... = new Foo; > or > ... = new Foo(); > > Just wondering because thy both work! They're both correct. Use whichever! |
Copyright © 1999-2021 by the D Language Foundation