www.digitalmars.com         C & C++   DMDScript  

D - Class access problems

reply "Andrew Edwards" <aedwards spamfreeamerica.com> writes:
The following class example taken from the documentation results in an
Access violation error when compiled and executed under WinXP. Does anyone
tell me what I'm doing wrong?

class Abc
{
  int myprop;
  int property(int newproperty) { myprop = newproperty; return 0;} // set'er
  int property() { return myprop; } // get'er
}

int main() {
  Abc a;
 a.property(3);
 int x = a.property();
 return 0;
}

Thanks,
Andrew
Jan 08 2003
parent reply Burton Radons <loth users.sourceforge.net> writes:
Andrew Edwards wrote:
 The following class example taken from the documentation results in an
 Access violation error when compiled and executed under WinXP. Does anyone
 tell me what I'm doing wrong?
 
 class Abc
 {
   int myprop;
   int property(int newproperty) { myprop = newproperty; return 0;} // set'er
   int property() { return myprop; } // get'er
 }
 
 int main() {
   Abc a;
  a.property(3);
  int x = a.property();
  return 0;
 }

a is initialised to null - you need to use "Abc a = new Abc;" to initialise it to an instance. This is a spec bug - Walter, it's at (http://www.digitalmars.com/d/class.html) under the Fields section.
Jan 08 2003
next sibling parent "Andrew Edwards" <aedwards spamfreeamerica.com> writes:
have get'ers and set'ers been implemented yet? Tried using them from the
original example but they don't seem to work

"Burton Radons" <loth users.sourceforge.net> wrote in message
news:aviak5$pe6$1 digitaldaemon.com...
 Andrew Edwards wrote:
 The following class example taken from the documentation results in an
 Access violation error when compiled and executed under WinXP. Does


 tell me what I'm doing wrong?

 class Abc
 {
   int myprop;
   int property(int newproperty) { myprop = newproperty; return 0;} //


   int property() { return myprop; } // get'er
 }

 int main() {
   Abc a;
  a.property(3);
  int x = a.property();
  return 0;
 }

a is initialised to null - you need to use "Abc a = new Abc;" to initialise it to an instance. This is a spec bug - Walter, it's at (http://www.digitalmars.com/d/class.html) under the Fields section.

Jan 08 2003
prev sibling parent reply Burton Radons <loth users.sourceforge.net> writes:
Burton Radons wrote:
 Andrew Edwards wrote:
 
 The following class example taken from the documentation results in an
 Access violation error when compiled and executed under WinXP. Does 
 anyone
 tell me what I'm doing wrong?

 class Abc
 {
   int myprop;
   int property(int newproperty) { myprop = newproperty; return 0;} // 
 set'er
   int property() { return myprop; } // get'er
 }

 int main() {
   Abc a;
  a.property(3);
  int x = a.property();
  return 0;
 }

a is initialised to null - you need to use "Abc a = new Abc;" to initialise it to an instance. This is a spec bug - Walter, it's at (http://www.digitalmars.com/d/class.html) under the Fields section.

Wait, sorry, never mind. This excerpted code is not D, it's C++, so you were trying it in the wrong language.
Jan 08 2003
parent reply "Andrew Edwards" <aedwards spamfreeamerica.com> writes:
Here is the code as it originally appeared on the D website:

All this is quite a bit of typing, and it tends to make code unreadable by
filling it with getProperty() and setProperty() calls. In D, get'ers and
set'ers take advantage of the idea that an lvalue is a set'er, and an rvalue
is a get'er:

class Abc
{ int myprop;
void property(int newproperty) { myprop = newproperty; } // set'er
int property() { return myprop; } // get'er
}

which is used as:

Abc a;
a.property = 3; // equivalent to a.property(3)
int x = a.property; // equivalent to int x = a.property()

It does not work. So I had to modify it so that it works somehow.  I hadn't
noticed the C++ example above it.  Thanks for pointing that out. The problem
still remains though.  This code does not work.

Thanks,
Andrew
"Burton Radons" <loth users.sourceforge.net> wrote in message
news:avic2o$qc6$1 digitaldaemon.com...
 Burton Radons wrote:
 Andrew Edwards wrote:

 The following class example taken from the documentation results in an
 Access violation error when compiled and executed under WinXP. Does
 anyone
 tell me what I'm doing wrong?

 class Abc
 {
   int myprop;
   int property(int newproperty) { myprop = newproperty; return 0;} //
 set'er
   int property() { return myprop; } // get'er
 }

 int main() {
   Abc a;
  a.property(3);
  int x = a.property();
  return 0;
 }

a is initialised to null - you need to use "Abc a = new Abc;" to initialise it to an instance. This is a spec bug - Walter, it's at (http://www.digitalmars.com/d/class.html) under the Fields section.

Wait, sorry, never mind. This excerpted code is not D, it's C++, so you were trying it in the wrong language.

Jan 08 2003
parent Burton Radons <loth users.sourceforge.net> writes:
Andrew Edwards wrote:
 Here is the code as it originally appeared on the D website:
 
 All this is quite a bit of typing, and it tends to make code unreadable by
 filling it with getProperty() and setProperty() calls. In D, get'ers and
 set'ers take advantage of the idea that an lvalue is a set'er, and an rvalue
 is a get'er:
 
 class Abc
 { int myprop;
 void property(int newproperty) { myprop = newproperty; } // set'er
 int property() { return myprop; } // get'er
 }
 
 which is used as:
 
 Abc a;
 a.property = 3; // equivalent to a.property(3)
 int x = a.property; // equivalent to int x = a.property()
 
 It does not work. So I had to modify it so that it works somehow.  I hadn't
 noticed the C++ example above it.  Thanks for pointing that out. The problem
 still remains though.  This code does not work.

Now this example IS a spec bug, as it's in D and doesn't initialise Abc. Anyway, as to your other question, properties aren't implemented in DMD yet.
Jan 08 2003