Given this code:
struct base
{
base() = default;
virtual ~base() = default;
base(const base&) = default;
base(base&&) = default;
base& operator=(const base&) = default;
base& operator=(base&&) = default;
virtual void debug(){ std::cout << "base debug\n"; }
};
struct derived : public base
{
public:
void debug() override { std::cout << "derived debug\n"; }
void derived_only() { std::cout << "derived only\n"; }
};
derived g_d; // NOLINT
void get_base(base &b)
{
b = g_d; // clang tidy complains about this, so, fair enough
}
This gives the following clang tidy warning:
But then if I assigned to a base reference first (pointers / refs are allowed to point to derived):
void get_base(base &b)
{
base &b_temp = g_d;
b = b_temp;
}
Then the clang-tidy warning goes away.
But my question is, is this really any different to the initial code - i.e. its just masking the issue, or is this a legitimate "work-around".
Ideally I would just pass pointers/references only and not take copies. But in this case it would be convenient to use this workaround.
The warning does actually say that the overridden function debug() is discarded - which is exactly what we want - we only want/need the base class members.
