1 NPRG041 Programming in C++ - 2023/2024 David

1 NPRG041 Programming in C++ - 2023/2024 David
1 / 1
1 NPRG041 Programming in C++ - 2023/2024 David - slide 1 of 12 1 NPRG041 Programming in C++ - 2023/2024 David - slide 2 of 12 1 NPRG041 Programming in C++ - 2023/2024 David - slide 3 of 12 1 NPRG041 Programming in C++ - 2023/2024 David - slide 4 of 12 1 NPRG041 Programming in C++ - 2023/2024 David - slide 5 of 12 1 NPRG041 Programming in C++ - 2023/2024 David - slide 6 of 12 1 NPRG041 Programming in C++ - 2023/2024 David - slide 7 of 12 1 NPRG041 Programming in C++ - 2023/2024 David - slide 8 of 12 1 NPRG041 Programming in C++ - 2023/2024 David - slide 9 of 12 1 NPRG041 Programming in C++ - 2023/2024 David - slide 10 of 12 1 NPRG041 Programming in C++ - 2023/2024 David - slide 11 of 12 1 NPRG041 Programming in C++ - 2023/2024 David - slide 12 of 12
1 NPRG041 Programming in C - 20232024 David Bednárek Values vs. references How does this work in your preferred language? x createbeast(100); print(x.health); 100 y x; does it create a copy or share a reference?

Related Topics

Download this presentation From Below

"1 NPRG041 Programming in C++ - 2023/2024 David" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Presentation Transcript

01
1 NPRG041 Programming in C++ - 2023/2024 David Bednárek Values vs. references<br>
02
How does this work in your preferred language?
x = create_beast(100);
print(x.health); // 100
y = x; // does it create a copy or share a reference?
y.damage_yourself(50); // y.health -= 50;
print(x.health); // 100 if copy, 50 if shared Value vs. reference types 2 NPRG041 Programming in C++ - 2019/2020 David Bednárek 100 Beast x 100 50 Beast y 100 50 Beast x y damage_yourself() damage_yourself()<br>
03
Note: The distinction is irrelevant for immutable types
In many languages (not in C++), strings are immutable
x = "Hell";
y = x; // is it a copy, deep copy, or shared reference?
// y.append("o"); we cannot tell because we cannot modify y in place
y = y.append("o"); // we only have this interface, returning a new object
Boxed primitive types (e.g. Integer in java) are usually immutable reference types
High-level languages always work with objects – numbers are immutable objects there
z = z + 1 // creates a new object (of type int) in python Immutable types 3 NPRG041 Programming in C++ - 2019/2020 David Bednárek Hell x y Hell x y Hell x y Hell Hello Hello Hello append() append() append() returns returns returns assignment assignment assignment Hell Hello<br>