x


Will this be an instance or a pointer?

Hiya all,

I'm just going to use a rough example here in pseudo code. Lets say that in Javascript I have an ObjectManager class:

class ObjectManager
{
// A singleton object
private var anObject : MyObject = new MyObject();

// Accessor method
static function getAnObject() : MyObject
{
    return anObject;
}
}

Now lets say I have a class (randomClass):

class randomClass
{
private var randomObject : MyObject;

function Start()
{
    anObject = ObjectManager.getAnObject();
}
}

The Question: Is the variable 'randomObject' a pointer to 'anObject', or is it a copy of 'anObject'?

more ▼

asked Apr 22 '10 at 01:04 PM

joeltebbett gravatar image

joeltebbett
148 14 14 23

interested I am!

Apr 22 '10 at 01:18 PM headkit
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Yes that is a simple implementation of singleton. As long as the instance of anObject is never replaced in ObjectManager it will continue to hand out that particular instance. Anything you do to the instance (any state changes) outside of the ObjectManager class will in fact be done to the private instance in the ObjectManager class. They are the same. There are no pointers in JS (the term of art is reference here), but I think that is what you mean.

more ▼

answered Apr 22 '10 at 01:32 PM

raypendergraph gravatar image

raypendergraph
1.6k 15 21 37

Also note that you can determine if two references point to the same instance on the heap with the == operator.

refOne = ObjectManager.getAnObject(); refTwo = ObjectManager.getAnObject();

Then refOne == refTwo should be true.

The story is a little different for strings in .NET but for this it's true.

Apr 22 '10 at 01:37 PM raypendergraph

Thank-you so much ... that is exactly what I needed (and wanted) to know ;)

Apr 22 '10 at 03:15 PM joeltebbett
(comments are locked)
10|3000 characters needed characters left

It's a reference to anObject. You can have multiple references, all referencing a single object

more ▼

answered Apr 22 '10 at 01:33 PM

Lucas Meijer 1 gravatar image

Lucas Meijer 1 ♦♦
7.9k 19 43 85

Thanks for the answer!

Apr 22 '10 at 03:16 PM joeltebbett
(comments are locked)
10|3000 characters needed characters left

Important: if you want it to be a singleton you'll need to declare the variable as static. Otherwise each instance of ObjectManager will have its own anObject, which is exactly the opposite of having a single one. (Also, ObjectManager won't compile unless the variable is static because it is being referred to in the static function getAnObject().

more ▼

answered Apr 22 '10 at 03:11 PM

Molix gravatar image

Molix
4.8k 15 27 66

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x202
x76
x32

asked: Apr 22 '10 at 01:04 PM

Seen: 1123 times

Last Updated: Apr 22 '10 at 01:04 PM