In C++, you may forget to refer to objects with heavy generation and release, such as std::set, when passing them to a function with const.Do you have any existing code review tools or compiler options to check this (now using GCC 7.3)?
Even if I'm careful, I'm in trouble because code reviews often warn me.
Make sure you turn off the copy constructor (and substitute operator).
For example, if you do not want to copy std::set<int>
, define the class that inherited std::set<int>
and clear the copy constructor and operator=
.
class IntSet:public std::set<int>{
public:
IntSet();
// Define other constructors as needed
IntSet(const IntSet&) = delete;
IntSet&operator=(const IntSet&)=delete;
};
This causes a compilation error if a copy of an instance is required for an argument that is not a reference.
If you need to copy intentionally, add a member function such as IntSetclone()const
.
© 2024 OneMinuteCode. All rights reserved.