C++ methods interesting definition | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C++ methods interesting definition

I see some functions in class is defined like void func() = default; or void func() const {}; I could not find the right keywords for searching this. How are those definitions called and what does it mean?

26th Apr 2021, 5:36 PM
Emre İRİŞ
Emre İRİŞ - avatar
2 Answers
+ 2
It is possible for the compiler to generate certain functions for you. That is what the "= default" is intended for. You can use it for example if you quickly want to add a default constructor to your class. There is also the "= delete" keyword, which will forbid use of the marked function and generate an error if it is called. https://www.bogotobogo.com/cplusplus/C11/C11_default_delete_specifier.php https://stackoverflow.com/questions/27232943/when-to-use-default-vs-delete The top answer in this thread gives an overview over what code the compiler generates for each defaulted method: https://stackoverflow.com/questions/563221/is-there-an-implicit-default-constructor-in-c/563320#563320 Constant methods are methods that do not change the object they are called from in any way. Only constant methods can be called from constant instances of a class. https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
26th Apr 2021, 5:56 PM
Shadow
Shadow - avatar
0
Thank you for clear explanation 🙂🙃🙃
26th Apr 2021, 6:32 PM
Emre İRİŞ
Emre İRİŞ - avatar