www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Need a little help (probably being stupid)

reply Dn7 <Dn7_member pathlink.com> writes:
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...
Jul 25 2004
next sibling parent reply Andrew Edwards <ridimz_at yahoo.dot.com> writes:
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;

 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...
 
 

Jul 25 2004
parent reply Dn7 <Dn7_member pathlink.com> writes:
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;

 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.
Jul 25 2004
parent Mike Parker <aldacron71 yahoo.com> writes:
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;
Jul 26 2004
prev sibling parent reply J C Calvarese <jcc7 cox.net> writes:
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/
Jul 25 2004
parent reply Andrew Edwards <ridimz_at yahoo.dot.com> writes:
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...


Jul 25 2004
next sibling parent J C Calvarese <jcc7 cox.net> writes:
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/
Jul 25 2004
prev sibling parent "Walter" <newshound digitalmars.com> writes:
"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!
Jul 26 2004