March 04, 2008
> Two things.
> First, a null reference is free to set up, whereas instantiating a class
> is not free.

But always writing an extra line isn't free and if nobody would use null
references that would be kind of stupid.
^^ thats what I thought (^_^)

>
> Second:
>
> ---
> class Bicycle {
>    Human owner;
> }
>
> auto bike = new Bicycle();
> ---
> The bike doesn't have an owner. Do you want to create a Human object that refers to nobody? Well, maybe, for some purposes, but not necessarily. That would probably alter your definition of Human.

bike would only hold a reference to an Human, right? and you say this is
bad?
Sorry, I read it as having only a reference is bad .. I must read this wrong
:/


March 04, 2008
> What is Class doesn't have a default constructor, or has many?

What is the difference between `first only having a reference and then
allocating` or `direct allocation` in those two cases?
Or can't the instance be allocated in those cases?
If they can't then what is the use of having only a reference to them?


March 04, 2008
Could anybody give a simple example of unallocated class reference use? I think that would explain it all to me :)


March 04, 2008
> Could anybody give a simple example of unallocated class reference use? I think that would explain it all to me :)

Class className; // This will create a reference
className=new Class(); // This will allocate an instance of Class

Why do I need to name the class twice?
Is there a shortcut?

And, could I do:

Class className;
className=new Class_2();

I know some of my questions might be interpreted as being rhetorical, when
they really aren't.
I am that unknowing :)


March 04, 2008
On Tue, 04 Mar 2008 16:56:15 +0100, Saaa wrote:

> Could anybody give a simple example of unallocated class reference use? I think that would explain it all to me :)

class Bike {
    Human owner;

    this(Human o) {
         owner = o;
    }
    public void newOwner(Human o) {
         owner = o;
    }
}

class Human {
    Bike bike;
    char[] name;

public:
   this(char[] n) { name = n; }

    void ride() {
         if(bike !is null) {
             writefln("%s is riding his bike", name);
         }
    }

    void purchase(Bike b) {
         b.newOwner(this);
    }
}

void main() {
    auto store = new Bike[10];
    store[] = new Bike(null);

    Human joe = new Human("Joe");
    joe.ride();

    // Joe buys a new bike
    joe.purchase(store[4]);
}

you will notice that the bike requires an owner, but I provided none during creation. Also note that a Human does not have to own a bike, would you want to force a creation of bike even though he has not purchased one? I didn't test the code, but I hope it works.

One of the things that happens as that you want a reference to an object type, but not create a new one, because later you will be getting the reference from somewhere else. Feel free to use what I have given you.
March 04, 2008
Thanks.

Why did you use:
    auto store = new Bike[10];
    store[] = new Bike(null);
iso:
    Bike[10] store;
    store[] = new Bike(null);

At first I thought that:
    auto store = new Bike[10];
would allocate the instances as well.


> class Bike {
>    Human owner;
>
>    this(Human o) {
>         owner = o;
>    }
>    public void newOwner(Human o) {
>         owner = o;
>    }
> }
>
> class Human {
>    Bike bike;
>    char[] name;
>
> public:
>   this(char[] n) { name = n; }
>
>    void ride() {
>         if(bike !is null) {
>             writefln("%s is riding his bike", name);
>         }
>    }
>
>    void purchase(Bike b) {
>         b.newOwner(this);
>    }
> }
>
> void main() {
>    auto store = new Bike[10];
>    store[] = new Bike(null);
>
>    Human joe = new Human("Joe");
>    joe.ride();
>
>    // Joe buys a new bike
>    joe.purchase(store[4]);
> }
>
> you will notice that the bike requires an owner, but I provided none during creation. Also note that a Human does not have to own a bike, would you want to force a creation of bike even though he has not purchased one? I didn't test the code, but I hope it works.
>
> One of the things that happens as that you want a reference to an object type, but not create a new one, because later you will be getting the reference from somewhere else. Feel free to use what I have given you.


March 04, 2008
Joe doesn't seem to get his bike :/

Is this how you meant it?

void purchase(Bike b) {
   b.newOwner(this);
   bike=b;
}


>
> class Bike {
>    Human owner;
>
>    this(Human o) {
>         owner = o;
>    }
>    public void newOwner(Human o) {
>         owner = o;
>    }
> }
>
> class Human {
>    Bike bike;
>    char[] name;
>
> public:
>   this(char[] n) { name = n; }
>
>    void ride() {
>         if(bike !is null) {
>             writefln("%s is riding his bike", name);
>         }
>    }
>
>    void purchase(Bike b) {
>         b.newOwner(this);
>    }
> }
>
> void main() {
>    auto store = new Bike[10];
>    store[] = new Bike(null);
>
>    Human joe = new Human("Joe");
>    joe.ride();
>
>    // Joe buys a new bike
>    joe.purchase(store[4]);
> }
>
> you will notice that the bike requires an owner, but I provided none during creation. Also note that a Human does not have to own a bike, would you want to force a creation of bike even though he has not purchased one? I didn't test the code, but I hope it works.
>
> One of the things that happens as that you want a reference to an object type, but not create a new one, because later you will be getting the reference from somewhere else. Feel free to use what I have given you.


March 04, 2008
On Tue, 04 Mar 2008 17:38:31 +0100, Saaa <empty@needmail.com> wrote:

>> Could anybody give a simple example of unallocated class reference use?
>> I think that would explain it all to me :)
>
> Class className; // This will create a reference
> className=new Class(); // This will allocate an instance of Class
>
> Why do I need to name the class twice?
> Is there a shortcut?


  Class className = new Class();

should work, as should

  auto className = new Class();


>
> And, could I do:
>
> Class className;
> className=new Class_2();

Depending on whether or not Class_2 is inherited from Class, this might work.
i.e, this works

class Foo{}
class Bar : Foo{}

Foo f = new Bar();

And this does not:

class FooBar{}
class BarFoo{}

FooBar f = new BarFoo();


> I know some of my questions might be interpreted as being rhetorical, when
> they really aren't.
> I am that unknowing :)

Hey, we all started out knowing nothing. :p

-- Simen
March 04, 2008
Just so you can ask that question, no not really but I'll tell you the difference.

auto store = new Bike[10];

Allocates memory on the heap, which means the function can return it.

Bike[10] store;

will allocate memory on the stack thus will not exist when the function returns. I had no reason not to use this, I just ended up not.

And both arrays are static, thus there length will not change.

A note to your other reply, you are correct, that was what I meant.

On Tue, 04 Mar 2008 18:19:47 +0100, Saaa wrote:

> Thanks.
> 
> Why did you use:
>     auto store = new Bike[10];
>     store[] = new Bike(null);
> iso:
>     Bike[10] store;
>     store[] = new Bike(null);
> 
> At first I thought that:
>     auto store = new Bike[10];
> would allocate the instances as well.
> 
> 
>> class Bike {
>>    Human owner;
>>
>>    this(Human o) {
>>         owner = o;
>>    }
>>    public void newOwner(Human o) {
>>         owner = o;
>>    }
>> }
>>
>> class Human {
>>    Bike bike;
>>    char[] name;
>>
>> public:
>>   this(char[] n) { name = n; }
>>
>>    void ride() {
>>         if(bike !is null) {
>>             writefln("%s is riding his bike", name);
>>         }
>>    }
>>
>>    void purchase(Bike b) {
>>         b.newOwner(this);
>>    }
>> }
>>
>> void main() {
>>    auto store = new Bike[10];
>>    store[] = new Bike(null);
>>
>>    Human joe = new Human("Joe");
>>    joe.ride();
>>
>>    // Joe buys a new bike
>>    joe.purchase(store[4]);
>> }
>>
>> you will notice that the bike requires an owner, but I provided none during creation. Also note that a Human does not have to own a bike, would you want to force a creation of bike even though he has not purchased one? I didn't test the code, but I hope it works.
>>
>> One of the things that happens as that you want a reference to an object type, but not create a new one, because later you will be getting the reference from somewhere else. Feel free to use what I have given you.
March 04, 2008
"Jesse Phillips" <jessekphillips@gmail.com> wrote in message news:fqkejg$1vkn$1@digitalmars.com...
> Just so you can ask that question, no not really but I'll tell you the difference.
>
> auto store = new Bike[10];
>
> Allocates memory on the heap, which means the function can return it.
>
> Bike[10] store;
>
> will allocate memory on the stack thus will not exist when the function returns. I had no reason not to use this, I just ended up not.
>
> And both arrays are static, thus there length will not change.

Nope.  new Bike[10] allocates a new dynamically-sized array of length 10; the type of that expression is Bike[], not Bike[10].  It's really sugar for new Bike[](10).  Thus its length can change.

It's not actually possible to allocate a statically-sized array on the heap directly.  You have to use a templated struct and allocate that.