Newbie question

Stewart Gordon smjg_1998 at yahoo.com
Tue Nov 28 12:25:33 PST 2006


Dune wrote:
<snip>
> This doesn't:
> 
> class Foo {
>   public:
>   int data() { return m_data; }                  // read property
>   int data(int value) { return m_data = value; } // write property
> 
>   private:
>   int m_data;
> }
> 
> void main() {
>   Foo f;
>   f.data = 3; // same as f.data(3);
<snip>

Welcome to D.

Classes in D have reference semantics.  That is, by declaring a Foo, you 
are declaring not an object, but a reference to one.  Initially, any 
reference is null, i.e. it doesn't refer to anything.  Before you can do 
anything with it, you must make it refer to an object.

     f = new Foo;

This creates a new object of type Foo, and makes f refer to it.  Once 
that's done, then you can do stuff with it.

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- 
PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.



More information about the Digitalmars-d-learn mailing list