Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

Are tables in class instances shared?

Last post 05-09-2008, 1:59 AM by auron0. 2 replies.
Sort Posts: Previous Next
  •  05-08-2008, 9:32 PM 2499

    Are tables in class instances shared?

    Hi,

    I've written this to test the behaviours of tables in classes and their instances.
    It seems that tables in a class are shared between all instances of that class.
    Is this intended? Shouldn't each class instance contain it's own instance of the table?
    Class instances seem to have their own instance of simple types, like integers.

    class Object
    {
        Attributes = {};
    }

    class ChildObject extends Object
    {
        constructor()
        {
            Attribute["1"] <- 0;
        }
    }


    print( ChildObject.Attributes );
    //print( ChildObject.Attributes["1"] );    // 1) This results in AN ERROR HAS OCCURED [the index '1' does not exist]

    local test = ChildObject();
    print( test.Attributes );                    // 2) This shows the same address as ChildObject.Attributes.
    print( test.Attributes["1"] );                // 2) This is 0, as expected
    // Change the instance's member.
    test.Attributes["1"] = 1;
    print( test.Attributes );                    // 3) Still the same address as ChildObject.Attributes.
    print( test.Attributes["1"] );                // 3) This is 1, as expected
    print( ChildObject.Attributes );
    print( ChildObject.Attributes["1"] );        // 4) But this is now also 1, when it should be 0

    //local x = 1+1;
    local test2 = ChildObject();
    print( test2.Attributes );                    // 5) This also shows the same address as ChildObject.Attributes.
    print( test2.Attributes["1"] );                // 5) This shows 0, as if ChildObject.Attributes was reset
    // Change the 2nd instance's member.
    test2.Attributes["1"] = 2;
    print( test.Attributes );                    // 6) Still the same address as ChildObject.Attributes.
    print( test.Attributes["1"] );                // 6) This now shows 2, when it should be 1
    print( test2.Attributes );                    // 7) Still the same address as ChildObject.Attributes.
    print( test2.Attributes["1"] );                // 7) This shows 2, as expected
    print( ChildObject.Attributes );
    print( ChildObject.Attributes["1"] );        // 8) This now shows 2, when it should be 0

  •  05-08-2008, 10:16 PM 2500 in reply to 2499

    Re: Are tables in class instances shared?

  •  05-09-2008, 1:59 AM 2501 in reply to 2500

    Re: Are tables in class instances shared?

    Thanks :)
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems