Sign up for the KDAB Newsletter
Stay on top of the latest news, publications, events and more.
Go to Sign-up
Find what you need - explore our website and developer resources
28 April 2022
Integers and strings can be used as data types for many different things: user id's, project id's, employee names, and account names, to name just a few.
Doing so, however, also means that you run the risk of confusing project ids with user ids or employee names with account names, with your compiler helplessly watching while you shoot yourself in the foot - after all to your compiler it is just integers and strings.
In this episode, I'll show one cure to this, namely a class you can wrap around your integers or strings to make them unique. I'll of course discuss the integration with Qt.
https://github.com/anthonywilliams/strong_typedef
If you have a Java background, a C++98 background or similar your finger memory may reach for the subclassing tool in your toolbox when faced with a problem.
In modern C++, there are many different ways to solve problems. In this episode, I'll show an example for which subclassing indeed isn't the right tool.
0:00 Introduction
0:26 The problem - compiler can't tell a city and a country apart
1:28 Trying to improve the situation with keyword "using"
2:32 Understanding the problem in more details
3:22 Introducing jss::strong_typedef
6:22 operator+ and qHash for ease of use
7:50 Adding own mix-ins
9:45 My experience in adding it to my own code base
The example code showcased in this video is available here: https://github.com/KDAB/kdabtv/tree/master/Qt-Widgets-and-more/strong_typedef
All QML tips and tricks videos : https://www.youtube.com/playlist?list=PL6CJYn40gN6jWHP5krsQrVGyYtKh3A3be
All Qt Widgets and more videos:
https://www.youtube.com/playlist?list=PL6CJYn40gN6gf-G-o6syFwGrtq3kItEqI