Leanpub Header

Skip to main content

C++ Initialization Story

A Guide Through All Initialization Options and Related C++ Areas

C++ Initialization Story
هذا الكتاب هو 100% كاملةأخر تحديث لهذا الكتاب كان فى 2023-06-19

From basic syntax, constructor and destruction rules, lifetime, and template deduction. Everything you want to know about Modern C++ techniques related to initialization. C++20 (and even C++23) included!

Buy together with my C++ Lambda book or with Lambda and C++17 books

Print version @Amazon.com

الحد الأدنى للسعر

$14.99

$19.99

أنت تدفع

مستحقات المؤلف

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
994
القراء
278
عدد الصفحات
55,473عدد الكلمات
حول

حول

نبذه عن الكتاب

Initialization in C++ is a hot topic! The internet is full of discussions about best practices, and there are even funny memes on that subject. The situation is not surprising, as there are more than a dozen ways to initialize a simple integer value, complex rules for the auto-type deduction, data members, and object lifetime nuances.

And here comes the book.

Throughout this text, you will learn practical options to initialize various categories of variables and data members in Modern C++. More specifically, this text teaches multiple types of initialization, constructors, non-static data member initialization, inline variables, designated initializers, and more. Additionally, you’ll see the changes and new techniques from C++11 to C++20 and lots of examples to round out your understanding.

Learn C++ from the perspective of C++ Initialization!

The plan is to explain most (if not all) parts of initialization, learn lots of excellent C++ techniques, and see what happens under the hood.

The goal of this book is to equip you with the following knowledge:

  • Explain rules about object initialization, including regular variables, data members, and non-local objects.
  • How to implement special member functions (constructors, destructors, copy/move operations) and when they are helpful.
  • How to efficiently initialize non-static data members using C++11 features like non-static data member initialization, inheriting, and delegating constructors.
  • How to streamline working with static variables and static data members with inline variables from C++17.
  • How to work with container-like members, non-copyable data members (like `const` data members) or move-able only data members, or even lambdas.
  • What is an aggregate, and how to create such objects with designated initializers from C++20.

The book contains 14 chapters in the following structure:

  • Chapters 1 to 5 create a foundation for the rest of the book. They cover basic initialization rules, constructors, destructors, and the basics of data members.
  • Chapter 6 on Type deduction - auto, decltype, AAA and more.
  • Chapter 7 is a quiz with 10 questions. You can check your knowledge from the first 6 chapters.
  • Chapter 8 describes Non-static Data Member Initialization (NSDMI), a powerful feature from C++11 that improves how we work with data members. At the end of the chapter, you can solve a few exercises.
  • Chapter 9 discusses how to initialize container-like data members.
  • Chapter 10 contains information about non-regular data members and how to handle them in a class. You'll learn about const data members, unique_ptr as a data member, and references.
  • Chapter 11 describes static non-local variables, static objects, various storage duration options, inline variables from C++17, and constinit from C++20.
  • Chapter 12 moves to C++20 and describes Designated Initializers, a handy feature based on similar thing from the C language.
  • Chapter 13 shows various techniques like passing strings into constructors, strong typing, CRTP class counter, Copy and swap idiom, self-registering types more.
  • Chapter 14 is the final quiz with questions from the whole book.

And there are two appendices:

  • Appendix A - a handy guide about rules for compiler-generated special member functions.
  • Appendix B - answers to quizzes and exercises.

Who is this book for?

The book is intended for beginner/intermediate C++ programmers who want to learn various aspects of initialization in Modern C++ (from C++11 to C++20).

You should know at least some of the basics of creating and using custom classes.

This text is also helpful for experienced programmers who know older C++ standards and want to move into C++17/C++20

See the reviews at:

>>  C++ Initialization Story by Bartłomiej Filipek @Goodreads

>> Adam Sawicki Coding Blog:Book Review

مشاركة هذا الكتاب

التصنيفات

الملاحظات

حزمتان

هذا الكتاب أيضا متاح فى هذه الحزمه:

المؤلف

نبذه عن المؤلف

Bartłomiej Filipek

Bartłomiej (Bartek) Filipek is a C++ software developer from the beautiful city of Cracow in Poland He started his professional coding career in 2007. In 2010 he graduated from Jagiellonian University in Cracow, Poland, with a Master's Degree in Computer Science.

Bartek currently works at Xara, where he develops features for advanced document editors. He also has experience with desktop graphics applications, game development, large-scale systems for aviation, writing graphics drivers and even biofeedback. In the past, Bartek has also taught programming (mostly game and graphics programming courses) at local universities in Cracow.

Since 2011 Bartek has been regularly blogging at bfilipek.com and cppstories.com. Initially, the topics revolved around graphics programming, but now the blog focuses on core C++. He's also a co-organiser of the C++ User Group in Cracow. You can hear Bartek in one @CppCast episode where he talks about C++17, blogging and text processing.

Since October 2018, Bartek has been a C++ Expert for the Polish National Body, which works directly with ISO/IEC JTC 1/SC 22 (C++ Standardisation Committee).

In the same month, Bartek was awarded his first MVP title for the years 2019/2020 by Microsoft.

In his spare time, he loves assembling Lego models with his little son.

See his blog at cppstories.com.

الترجمة

الترجمة

آراء العملاء

شهادات القراء

المحتويات

جدول المحتويات

About the Book

  1. Why should you read this book?
  2. Learning objectives
  3. The structure of the book
  4. Who is this book for?
  5. Prerequisites
  6. Reader feedback & errata
  7. Example code
  8. Formatting & Layout

About the Author

Acknowledgements

Revision History

1.Local Objects

  1. Starting with custom type
  2. Setting values to zero
  3. Copy and direct initialization
  4. Initialization with aggregates
  5. Default data member initialization
  6. Summary

2.Initialization With Constructors

  1. Defining a class
  2. Basics of constructors
  3. Body of a constructor
  4. Adding constructors to DataPacket
  5. Compiler-generated default constructors
  6. Explicit constructors and conversions
  7. Difference between direct and copy initialization
  8. Implicit conversion and converting constructors
  9. Constructor summary

3.Copy and Move Operations

  1. Copy constructor
  2. Move constructor
  3. Distinguishing from assignment
  4. Adding debug logging to constructors
  5. Trivial classes and user-provided default constructors
  6. Summary

4.Delegating and Inheriting Constructors

  1. Delegating constructors
  2. Limitations
  3. Inheritance
  4. Inheriting constructors
  5. Summary

5.Destructors

  1. Basics
  2. Objects allocated on the heap
  3. Destructors and data members
  4. Virtual destructors and polymorphism
  5. Partially created objects
  6. A compiler-generated destructor
  7. Summary

6.Type Deduction and Initialization

  1. Deduction with auto
  2. About value categories in C++
  3. Rules for auto type deduction
  4. Deduction with decltype
  5. Printing type info
  6. Structured bindings since C++17
  7. Template Argument Deduction for Class Templates
  8. Lifetime extension, references, and loops
  9. Almost Always Auto
  10. Summary

7.Quiz from Chapters 1…6

8.Non-Static Data Member Initialization

  1. How it works
  2. Investigation
  3. Experiments
  4. Other forms of NSDMI
  5. Copy constructor and NSDMI
  6. Move constructor and NSDMI
  7. C++14 changes
  8. C++20 changes
  9. Limitations of NSDMI
  10. NSDMI: Advantages and Disadvantages
  11. Summary

9.Containers as Data Members

  1. The basics
  2. Using std::initializer_list
  3. Example implementation
  4. The cost of copying elements
  5. Some inconvenience - non-copyable types
  6. More options (advanced)
  7. Summary

10.Non-regular Data Members

  1. Constant non-static data members
  2. Pointers as data members
  3. Smart pointers as data members
  4. References as data members
  5. Summary

11.Non-local objects

  1. Storage duration and linkage
  2. Initialization of non-local static objects
  3. constinit in C++20
  4. Static variables in a function scope
  5. About static data members
  6. Motivation for inline variables
  7. Global inline variables
  8. constexpr and inline variables
  9. Summary

12.Aggregates and Designated Initializers in C++20

  1. Aggregates as of C++20
  2. The basics of designated initializers
  3. Rules for designated initializers
  4. Advantages of designated initialization
  5. Examples
  6. Summary

13.Techniques and Use Cases

  1. Using explicit for strong types
  2. Best way to initialize string data members
  3. Reducing extra copies through emplace and in_place
  4. The copy and swap idiom
  5. CRTP class counter
  6. Several initialization types in one class
  7. Meyers Singleton and C++11
  8. Factory with self-registering types and static initialization
  9. Summary

14.The Final Quiz And Exercises

  1. Exercises

Appendix A - Rules for Special Member Function Generation

  1. The diagram
  2. Rule of zero
  3. Rule of three (deprecated!)
  4. Rule of 5 and 6 - modern C++
  5. Moveable-only types
  6. Polymorphic base classes

Appendix B - Quiz and Exercises Answers

References

احصل على الفصول النموذجية المجانية

Click the buttons to the right to get the free sample in PDF or EPUB, or read the sample online here

Leanpub بدون شروط, بدون مجازفه, 100% ضمان لسعادتك معنا

فى خلال 60 يوم من الشراء تستطيع أن تسترد 100% من الذى دفعته فى أى مشتريات من Leanpub , بنقرتين من الماوس. ألقى نظره على كامل الشروط.

اكسب 8$ على كل عملية شراء بقيمة 10$، و16$ على كل عملية شراء بقيمة 20$

نحن ندفع عائدات بنسبة ٨٠٪ على المشتريات التي تبلغ قيمتها 7.99$ أو أكثر، وعائدات بنسبة ٨٠٪ مع خصم رسوم ثابتة قدرها 50 سنتاً على المشتريات التي تتراوح بين 0.99$ و7.98$. تكسب 8$ على كل بيع بقيمة 10$، و16$ على كل بيع بقيمة 20$. لذلك، إذا بعنا 5000 نسخة غير مستردة من كتابك بسعر 20$، ستكسب 80,000$.

(نعم، بعض المؤلفين قد كسبوا بالفعل أكثر من ذلك بكثير على Leanpub.)

في الواقع، كسب المؤلفون أكثر من 14 مليون دولار من خلال الكتابة والنشر والبيع على Leanpub.

تعرف على المزيد حول الكتابة على Leanpub

تحديثات مجانية. خالي من حماية DRM.

إذا اشتريت كتابًا من Leanpub، فستحصل على تحديثات مجانية طالما يقوم المؤلف بتحديث الكتاب! يستخدم العديد من المؤلفين Leanpub لنشر كتبهم أثناء الكتابة. جميع القراء يحصلون على تحديثات مجانية، بغض النظر عن متى اشتروا الكتاب أو كم دفعوا (بما في ذلك مجانًا).

معظم كتب Leanpub متوفرة بتنسيق PDF (للكمبيوتر)، EPUB (للهواتف والأجهزة اللوحية و Kindle). يتم عرض التنسيقات التي يتضمنها الكتاب في الزاوية العلوية اليمنى من هذه الصفحة.

وأخيرًا، الكتب من Leanpub لا تحتوي على أي هراء حماية حقوق النسخ DRM، لذا يمكنك قراءتها بسهولة على أي جهاز مدعوم.

تعرف على المزيد حول تنسيقات الكتب الإلكترونية من Leanpub وأماكن قراءتها

اكتب وانشر على Leanpub

يستخدم المؤلفون والناشرون Leanpub لنشر كتب إلكترونية رائعة قيد التقدم والمكتملة، تمامًا مثل هذه. يمكنك استخدام Leanpub للكتابة، والنشر وبيع كتابك أيضًا! Leanpub هو منصة قوية للمؤلفين الجادين، يجمع بين تدفق الكتابة والنشر البسيط والأنيق مع متجر يركز على بيع الكتب الإلكترونية قيد التقدم. Leanpub هو آلة كتابة سحرية للمؤلفين: فقط اكتب بنص عادي، ولنشر كتابك الإلكتروني، فقط اضغط على زر. إنه حقًا بهذه السهولة.

تعرف على المزيد حول الكتابة على Leanpub