C++ Coding: Manipulating The Good Ole Rule Of Three. - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › C++ Coding: Manipulating The Good Ole Rule Of Three. (1235 Views)
| C++ Coding: Manipulating The Good Ole Rule Of Three. by Nobody: 11:18pm On Nov 23, 2018 |
So I conducted a research recently and was able to put up some deeply useful tips about how to implement the Rule of three carefully in C++. Though the rule of five is taking over in exception safety, it's still good to know this stuff. The rule of three is a mathematical rule with tech and even philosophical translations, and it's Google-defined as, "a method of finding a number in the same ratio to a given number as exists between two other given numbers." The Rule of three is wonderfully useful for writing exception-safe codes in C++, along with the rule of five. The rule of three states, that the copy constructor, the assignment operator and the destructor must always go together as member functions. A class that defines one must always define the other two. Major tip I tested out: If you manage dynamically-allocated resources with smart pointers, you won’t actually need a custom destructor. The smart-pointer object will take care of deletion for you. You really only need a custom copy-constructor and a custom copy-assignment operator. The rule of three is getting out of date with the rule of five, but I thought the tip might be good to share, for C++ers. The rule of three is wonderful when you want to modify an object data, so you append updates of the copy constructor and assignment operator temporarily. |
| Re: C++ Coding: Manipulating The Good Ole Rule Of Three. by best827(m): 7:19pm On Nov 24, 2018 |
How can I get in touch with you?? |
| Re: C++ Coding: Manipulating The Good Ole Rule Of Three. by elfico(m): 8:11am On Nov 25, 2018 |
C++ my first love. One has to be careful with pointers though. Improper use could have serious implications for you programs. First I am hearing of Rule of Three and Rule of Five. Thumbs up for the info. |
| Re: C++ Coding: Manipulating The Good Ole Rule Of Three. by Hibrahym: 7:59am On Nov 30, 2018 |
computergeek:That had been known way before C++11 smart pointers (unique_ptr, shared_ptr ...). That's the point of RAII. The rule of three states, that the copy constructor, the assignment operator and the destructor must always go together as member functions. A class that defines one must always define the other two.Destructor is only needed - for cleanups: when you dynamically allocate, freeing external resources etc. - or when u need destruction to be done in a certain way. Otherwise it's pretty useless defining a dtor. |
*#disclaimer Notice*: Ole Group Scam!!! • Chatgpt AI Revolution The Good And The Bad • Don't Wait For Enormous Capital, Rule The World Now With Your Own Fintech App • 2 • 3 • 4
Seeking A Web Mentor • My Fellow Java Programmers Might Like This,its Not A Main Class Though • Can Anyone Help On AT Commands