Reader small image

You're reading from  C++ Programming for Linux Systems

Product typeBook
Published inSep 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781805129004
Edition1st Edition
Languages
Right arrow
Authors (2):
Desislav Andreev
Desislav Andreev
author image
Desislav Andreev

Desislav Andreev is a software engineer with a PhD in artificial intelligence systems and quantum machine learning. He has several publications in software engineering and AI applications. For his 10 years in the field he has a demonstrated history of working in automotive software engineering and in the area of the higher education. He is skilled in system and software architectures, operating systems, C and C++ development, autonomous driving and computer graphics. He is currently working as a Lead C++ Developer in VMware, developing its core software functionalities. He is also a lecturer at the Technical University of Sofia. He was previously a Technical Professional and software architect in the CRE and ADAS departments of Visteon Corporation, working closely with both OEMs and development teams.
Read more about Desislav Andreev

Stanimir Lukanov
Stanimir Lukanov
author image
Stanimir Lukanov

Stanimir Lukanov is a C++ expert, software tech lead and architect at VMWare. He has more than 15 years of professional experience in writing efficient and robust C++ enterprise code. Stanimir is a member of the Bulgarian National Body which is part of The C++ Standards Committee (JTC1/SC22/WG21). His interests are in the area of software security for distributed enterprise software systems. Since 2017 he has worked at VMWare where he currently leads a team which develops core security functionality in one of the major products in the company's portfolio. Before joining VMWare he held the position of senior software engineer at Visteon Corporation and Johnson Controls. He was responsible for defining software architecture, making code reviews, leading C++ training and delivering fast and robust C++ code for real-time automotive embedded systems.
Read more about Stanimir Lukanov

View More author details
Right arrow

Diving Deep into the C++ Object

In this chapter, we will pay special attention to the object in the C++ language. But what makes the object in C++ so special that we should pay so much attention to it? Well, considering the fact that C++ supports the object-oriented programming paradigm, it is assumed that the object itself takes a central position in the structure of the language. You will see that there are many specifics around objects in C++.

In this chapter, we will delve into the fundamental aspects of objects in C++. We will start by examining how the C++ standard specifies the definition of an object. Moving on from there, we will take a closer look at the different types of object initializations, such as aggregate, direct, and copy initialization, and their use cases.

We will also explore the concept of storage duration for objects. Additionally, we will take a look at the scope and lifetime of objects in C++. We will also see what references are and how they correlate...

Technical requirements

All examples in this chapter have been tested in an environment with the following configuration:

Understanding the C++ object model

C++ programs involve the creation, manipulation, and destruction of various entities known as objects. An object in C++ possesses several attributes such as type, size, storage duration, lifetime, alignment requirements, and value. The name of the object is optional.

The lifetime of the named object is bounded by its storage duration, and if the object doesn’t have a name, it is considered a temporary object. However, not all entities in C++ are considered objects. For example, the reference is one such non-object.

First, let’s take a brief look at the terminology because it is important to be aware of it, as it will help us in our daily work with the C++ language.

Declaration versus definition

In C++, the terms declaration and definition are often used to refer to different aspects of a variable, function, or class. Here’s what each term means:

  • Declaration: A declaration introduces a name into a program and...

Functors and lambdas

This section will delve into functional objects – their definition, usefulness, and proper usage. We’ll begin by examining an example of a functional object used with an STL algorithm and discuss potential issues, such as the creation of temporary objects and dangling references. After that, we’ll move on to exploring lambda expressions – what they are, how to use them, and specific situations where they can be especially advantageous.

Exploring functional objects

In the Scope, storage duration, and lifetimes section, we looked at various types of object initialization in C++, but our focus was mostly on objects that represent data, such as integers or coordinates. In this section, we’ll shift our attention to another type of object – those designed to be callable, such as a function, but with a crucial difference: they can maintain a state between different function calls. These objects are known as functional objects...

Summary

In this chapter, we explored various aspects of objects in C++, including storage duration, scope, and lifetimes. We distinguished between objects and references and discussed different ways of initializing objects and when these initializations occur. Additionally, we delved into the world of functors, gaining an understanding of what they are and how to use them effectively. Building on that knowledge, we also learned about lambda expressions and their advantages over functors. We covered how to properly use both lambdas and functors with STL algorithms. Armed with this knowledge of object specifics, we can now move on to discussing error handling in C++ in the next chapter.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
C++ Programming for Linux Systems
Published in: Sep 2023Publisher: PacktISBN-13: 9781805129004
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Desislav Andreev

Desislav Andreev is a software engineer with a PhD in artificial intelligence systems and quantum machine learning. He has several publications in software engineering and AI applications. For his 10 years in the field he has a demonstrated history of working in automotive software engineering and in the area of the higher education. He is skilled in system and software architectures, operating systems, C and C++ development, autonomous driving and computer graphics. He is currently working as a Lead C++ Developer in VMware, developing its core software functionalities. He is also a lecturer at the Technical University of Sofia. He was previously a Technical Professional and software architect in the CRE and ADAS departments of Visteon Corporation, working closely with both OEMs and development teams.
Read more about Desislav Andreev

author image
Stanimir Lukanov

Stanimir Lukanov is a C++ expert, software tech lead and architect at VMWare. He has more than 15 years of professional experience in writing efficient and robust C++ enterprise code. Stanimir is a member of the Bulgarian National Body which is part of The C++ Standards Committee (JTC1/SC22/WG21). His interests are in the area of software security for distributed enterprise software systems. Since 2017 he has worked at VMWare where he currently leads a team which develops core security functionality in one of the major products in the company's portfolio. Before joining VMWare he held the position of senior software engineer at Visteon Corporation and Johnson Controls. He was responsible for defining software architecture, making code reviews, leading C++ training and delivering fast and robust C++ code for real-time automotive embedded systems.
Read more about Stanimir Lukanov