Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,513 members, 7,954,940 topics. Date: Saturday, 21 September 2024 at 12:59 PM |
Nairaland Forum / Science/Technology / Programming / C++ Coding: Manipulating The Good Ole Rule Of Three. (1173 Views)
CODELAGOS: 337 Schools To Participate In Coding Competition / Learn Coding For Free, With Microverse, Pay The Tuition When You Start Earning. / Do We Have Coding Bootcamps In Nigeria? / Faster Way To Learn Web Development. (2) (3) (4)
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. |
(1) (Reply)
React Styling(experienced Devs needed) / The Beauty Of Html, Css And Javascript. / What It Takes To Be A Real Programmer
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 43 |