Reputation: 147
Is there a concept of pointers or anything similar to that in as3?
I have used a concept of
var mc1:MovieClip;
var mc2:MovieClip();
var mcCommon:MovieClip = new MovieClip();
mcCommon["myptr"] = mc1;
mcCommon["myptr"] = mc2;
Where would I get to read and learn more about this concept. I'd like to get simple answers, as I am not an expert in as3. I'm still learning.
Upvotes: 2
Views: 4908
Reputation: 1768
If you are asking specifically about the code, first of all it should look like this:
var mc1:MovieClip;
var mc2:MovieClip;
var mcCommon:MovieClip = new MovieClip();
mcCommon["myptr"] = mc1;
mcCommon["myptr"] = mc2;
Though both mc1 and mc2 are actually null.
The syntax mcCommon["myptr"] means that you create a property named "myptr" on MovieClip mcCommon. So you can later retrieve the content of this property using mcCommon["myptr"]. The last line actually overwrites the content of the property (if it wasn't null of course). You can do that because MovieClip class is dynamic. On other (non-dynamic) instances you can't create properties like this.
Upvotes: 2
Reputation: 19046
I haven't coded in a language that specifically uses pointers in a while, and I'm not sure this is exactly what you're asking, but here's an attempt at answering your question:
In AS3, a variable reference to Objects are always pointers, with the exception of basic types (Strings, Numbers, ints, and Booleans), which are always by-value only.
So, for basic types, you get a "by-value" behavior:
var a:String = "hello";
var b:String = a; // b is a new String, a clone of a
b += " world";
trace(a); // hello
trace(b); // hello world
A modification to b did not modify the a variable. Or perhaps it could be said that a and b were references to the same String after line 2, but the += operator created a new String and assigned it to b. If you look at the String documentation, note that none of the member functions actually modify the String in-place, they all return new Strings.
You'll see the same behavior with function calls with simple, non-pointer parameters:
function wont_modify(word:String):String
{
word += " world";
return word;
}
var a:String = "hello";
trace(wont_modify(a)); // hello world
trace(a); // hello
However, for non-simple types, access is by-reference (what is referred to as a pointer in other languages):
var a:Array = [1, 2, 3];
var b:Array = a; // b is now a reference to a
b.push(4);
trace(a); // [1, 2, 3, 4]
trace(b); // [1, 2, 3, 4]
If you want a "pointer-like" behavior for a simple type like a String, it must be a member of a pointer-like Object, for which you pass a reference around:
function will_modify(obj:Object):void
{
obj.a += " world";
}
var obj:Object = { a:"hello" }
will_modify(obj);
trace(obj.a); // hello world
Strings and Numbers actually do actually extend Object, but all the operators and function calls treat them by-value and not by-reference.
Upvotes: 8
Reputation: 6282
Although not the same thing, I think what you are looking for is a Dictionary, where every property in the collection has a String
as a key which it can be accessed through.
Upvotes: 1