Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
January 08, 2003 Class access problems | ||||
---|---|---|---|---|
| ||||
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 |
January 08, 2003 Re: Class access problems | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Edwards | 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. |
January 08, 2003 Re: Class access problems | ||||
---|---|---|---|---|
| ||||
Posted in reply to Burton Radons | 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 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. > |
January 08, 2003 Re: Class access problems | ||||
---|---|---|---|---|
| ||||
Posted in reply to Burton Radons | 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.
|
January 09, 2003 Re: Class access problems | ||||
---|---|---|---|---|
| ||||
Posted in reply to Burton Radons | 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. > |
January 09, 2003 Re: Class access problems | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Edwards | 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.
|
Copyright © 1999-2021 by the D Language Foundation