简体   繁体   中英

Lua - Multiple objects, linked values?

I recently looked up an example of simple Lua oop principles and modified it slightly, as seen below.

What I find trouble comprehending is the connection between elf.name and hobbit.name. Why is it that when I change the value of either, that it affects the other? I am aware that I could have set elf.name as local inside the function, but it wouldn't have had the same effect.

In contrast, changing the value of another.name has no effect on the other two. Is there a lasting connection between elf.name and hobbit.name? I thought they were treated as separate objects.

Thanks.

;^) Zalokin

elf = {}

elf.name = "Frodo"

another = {}


function Character()

  return elf

end


local hobbit = Character()

print ("elf.name set to Frodo")

print("hobbit.name - "..hobbit.name)

print("elf.name - "..elf.name.."\
  ")


hobbit.name = "Charlie"

print ("hobbit.name set to Charlie")

print("hobbit.name - "..hobbit.name)

print("elf.name - "..elf.name.."\
  ")


another.name = "Gary"

print ("hobbit.name set to Charlie and another.name set to Gary")

print("hobbit.name - "..hobbit.name)

print("elf.name - "..elf.name)

print("another.name - "..another.name.."\
  ")

Result: -

>>>>elf.name set to Frodo
>>>>hobbit.name - Frodo
>>>>elf.name - Frodo
>>>>
>>>>hobbit.name set to Charlie
>>>>hobbit.name - Charlie
>>>>elf.name - Charlie
>>>>
>>>>hobbit.name set to Charlie and another.name set to Gary
>>>>hobbit.name - Charlie
>>>>elf.name - Charlie
>>>>another.name - Gary

Any use of {} , is known as a table constructor . It creates a whole new table. When you do elf.name = "Frodo" , you're modifying the table that elf points to. In your code, elf and another are initialized separately. On the other hand, hobbit is indirectly given a reference to elf . In other words, elf and hobbit are references to the same table.

function Character()

  return elf

end

local hobbit = Character()

That's what you are wrong at. I belive lua is pass-by-reference. This way, your code doesn't work. Also, Hobbit shouldn't be instance of Elf - if Lua is pass-by-reference its natural that instances will share data. Also, at top elf's name is Frodo. I recommend you to remove it. All you need to do is do this like you did with another object.

EDIT: Lua IS pass-by-reference but only on tables and objects. Quoting Lua 5.1 Reference Manual:

There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table. ....

Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM