Back to Basics: Designing Classes (part 2 of 2) - Klaus Iglberger - CppCon 2021

preview_player
Показать описание
---

Designing good, maintainable classes is a challenge. Sometimes it even feels like there is a decision to make in every single line of code. This talk will help you master this challenge. It will explain …

* … why small classes are beautiful;
* … why it is so important to encapsulate variation points;
* … why inheritance is rarely the answer for customization;
* … how to write good and maintainable constructors;
* … how to make sure class invariants are maintained;
* … how to handle member data;
* … how to write good member functions;
* … how to write good supporting functions;
* … why your private members are not private at all.

---
Klaus Iglberger

---

*--*
Рекомендации по теме
Комментарии
Автор

Both parts were bloody great, thank you for your work Klaus!

fillername
Автор

These 2 talks are the things that I have had to find in many different places over time but now conveniently and wonderfully presented here. Thanks again Klaus!

mehtubbhai
Автор

If you initialize in a different order than you declare, it is not a compilation error. The order of your initialization statements is simply ignored and the compile initializes in the same order that you declared. Think of it like the compile reorders your initializer statements to match the order of the declarations. It will warn you because the statements will be executed in a different order from how they are listed in the code, and this can be confusing for someone that doesn't understand this nuance, especially if the initialization of certain members has side effects outside of the class, or if one member is initialized based on the value of another member. For example, in the following class, m_P2 will always be uninitialized after the default constructor is called, even though it looks like it should point to m_Data like m_P does:
struct MyClass {
int m_Data, *m_P2, *m_P;
MyClass() : m_Data(5), m_P(&m_Data), m_P2(m_P) {};
};

Dziaji
Автор

This talk was kinda eye opening. A lot of stuff i have never thought about, like ordering the members.
Thank you very much!

artus
Автор

This guy is amazing. Such clarity. Great instructor.

kabasakalis
Автор

This one is absolute practical gem! Just because these guideline can and should be used for already existing code base.

LiveseyMD
Автор

Another piece of gold from Klaus! I follow this from part 1 and it does not let me down.
I will definitely go through the Cpp Guidelines, it should be a gold mine there to dig.

Thank Klaus and CppCon for making this available.

lehaipham
Автор

Great talk! I didn't knew the difference between visibility and accessibility till now.

AvijeetMaurya
Автор

Great talk, Klaus. Very insightful. Thank you

fouksmam
Автор

People playing with Pahole in Godbolt will appreciate -Wpadded compiler option as well. Thanks for the talk!

samuelpolacek
Автор

Really enlightening talk.Thanks a lot cppcon and klaus!

MasteringCodeInterviews
Автор

very neat explanation. expecting more talks.

sanjaygatne
Автор

The most fancy thing that I saw in C++ is try/catch for initialiser list.

#include <stdio.h>

class C
{
public:
C() try : a()
{ puts("C()"); }
catch(...)
{ puts("WOW!"); }
int a;
};

int main()
{
C c;
return 0;
}

konstantinburlachenko
Автор

Thank you for a very detailed and informative session. So much to learn!

rahul_walse
Автор

Also, bitfields are dangerous when it comes to concurrency (being accessed from multiple threads): Bitfields are usually not "separate memory locations". @2.27

MagnificentImbecil
Автор

Nice presentation, easy to follow and understand.

greob
Автор

@Klaus. If you had yr choice of programming styles in a new bluesky project, would you still prefer to use free functions over member functions?

The "Free your Functions!" talk you gave a while back settled it for me ;)

mehtubbhai
Автор

Kindly post a link to Core Guidelines !!!!

badassopenpolling
Автор

My new cppcon friend's other sleeve slides up accidentally.
Me: *gasp* "The betrayal!"

skadragons
Автор

At 48:00. What happens if you change the iterator to return a reference? Then calling code that dereferences the iterator will now break?

masheroz