As many have already pointed out the differences in copying structs and classes, it can all be understood from where they come from in c, a struct like
struct A {
let a: Int
let c: Bool
}
in memory local to the func parent object or struct it will be something like
64bit for int
8 bytes for bool
now for
class A {
let a: Int
let c: Bool
}
instead of the contents of the data being stored in local memory or struct or class it will be a single pointer
64bit address of class A instance
When you copy the two its easy to see why there are difference, copy the first, you copy the 64bit for the int and the 8 bit for the bool, copy the second you copy the 64bit address to the instance of class A, you can have multiple copies of the same memory address, all pointing to the same instance, but each copy of the struct will be its own copy.
Now things can get complicated because you can mix the two you have to something like
struct A {
let a: ClassA
let c: Bool
}
your memory will look something like
64bit address of class A instance
8 bytes for bool
This is a problem because even though you have multiple copies of the struct in your program, they all have a copy to the same object ClassA, this means just like multiples reference to instance ClassA you pass around have to have a reference count kept of how many reference to the object exists to know when to delete them, you program can have multiple references to struct A that need to keep a reference count to their ClassA instances, this can be time consuming if your struct has a lot of classes in them, or the structs it contains has lots of classes in them, now when you copy your struct, the compiler has to generate code that goes through every single class instance referenced in your struct and substructs, and increment there reference count to keep track of how many references there are. This can make classes much faster to pass around as you just need to copy its single address, and it won't need to increase the reference count of any of its children because it want reduce the reference count of any child it contains until its own reference count reaches 0.
The thing gets even more complicated with some Apple struct types, that they actually have object types in them, the good thing about data that is reference to, is it can be stored in memory and be lengthened and contractor at will and they can be very large, unlike data stored on local stack, so types like String, Array, Set, Dictionary though they act like struct and will even make a duplicate of there internal data if you try to modify them so you don't change all occurrence, there data still has to be reference counted and so a struct containing a lots of these types can still be slow, because the internal data for each one has to be retained.
Of cause passing struct types can reduce the possibility of lots of error, but they can also slow your program down depending on the types the contain.
void my_func(int a)
vsvoid my_func(int &a)
. This is a very fundamental question of programming. Read more: stackoverflow.com/questions/373419/… – Subteen