This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
The Land Of Objects
Welcome to the land of objects. In this land, objects say their first name when their lifetime begins, and say their last name when their lifetime ends.
We have four different types of object lifetimes that we will eventually encounter.
- automatic
- static
- thread_local
- dynamic
Each type of lifetime has its own rules for when the lifetime begins and lifetime ends, and well explain them as we get to them!
Object Declaration
Every object in our land looks like this:
S
object_1
(
"First Name"
,
"Last Name"
);
S
: The objects Typeobject_1
: The objects ID"First Name"
: The objects first name"Last Name"
: The objects last name
Important note:
Anything starting with //
is a comment and has no impact on the code!
A Note On Puzzle Layout
Some puzzles split across pages. Sometimes this is annoying. Ive decided to leave it how it is because it adds a sense of realism to what its actually like to read unnecessarily complex C++ code.
How C++ Relates
Each of these examples are real C++ code. If you are a C++ user, you can learn more about object lifetime with these puzzles.
These puzzles come directly from classes that I teach with C++ programmers. If you find them interesting, contact me about training. https://articles.emptycrate.com/training.html
Each solution is a topic that has something to do with C++. If you cannot figure out what the solution means, then duckduckgo for it and learn something new :).
If you arent a C++ user then dont worry about any of this, just have fun!
Automatic Lifetime
Automatic lifetime is the default. Objects with automatic lifetime have their lifetime begin
when their ID is first seen, and their lifetime end when their ID is no longer visible (after the ending }
).
void
run
()
{
S
object_1
(
"1"
,
"2"
);
}
When this code is run, this is what happens:
void
run
()
{
S
object_1
(
"1"
,
"2"
);
// 1 is printed
}
// 2 is printed, object_1 is no longer visible
The output is:
12
Objects lifetimes end in the reverse order they begin:
void
run
()
{
S
object_1
(
"1"
,
"2"
);
S
object_2
(
"3"
,
"4"
);
}
When this code is run, this is what happens:
void
run
()
{
S
object_1
(
"1"
,
"2"
);
// 1 is printed
S
object_2
(
"3"
,
"4"
);
// 3 is printed
}
// 4 is printed, object_2's lifetime ends first
// 2 is printed, object_1's lifetime ends second
The output is:
1342
We can introduce a new scope with a {
. Now remember, an automatic objects lifetime
ends when its ID is no longer visible. When it goes out of scope.
void
run
()
{
{
S
object_1
(
"1"
,
"2"
);
}
S
object_2
(
"3"
,
"4"
);
}
When this code is run, this is what happens:
void
run
()
{
{
S
object_1
(
"1"
,
"2"
);
// 1
}
// 2
S
object_2
(
"3"
,
"4"
);
// 3
}
// 4
The output is:
1234
Automatic Lifetime Puzzles
Puzzle 1
void
run
()
{
S
object_1
(
"p"
,
"s"
);
S
object_2
(
"l"
,
"u"
);
}
Answer
(
4
)
:
__
__
__
__
Puzzle 2
void
run
()
{
{
S
object_1
(
"s"
,
"l"
);
{
S
object_2
(
"t"
,
"r"
);
}
S
object_3
(
"t"
,
"o"
);
}
}
Answer
(
6
)
:
__
__
__
__
__
__
Puzzle 3
void
run
()
{
{
{
S
object_1
(
"p"
,
"a"
);
}
S
object_2
(
"c"
,
"k"
);
}
S
object_3
(
"a"
,
"d"
);
S
object_4
(
"g"
,
"e"
);
}
Answer
(
8
)
:
__
__
__
__
__
__
__
__
Puzzle 4
void
run
()
{
{
S
object_1
(
"w"
,
"e"
);
}
{
S