One can inherit all constructors of a base class using following code:
class DerivedClass : public BaseClass {
public:
using BaseClass::BaseClass;
...
};
Is it possible to inherit all operators in simillar way (one line)?
Paticular code that does not compile:
#include <string>
#include <memory>
using std::string;
struct A : string
{
using string::string;
};
int main()
{
std::shared_ptr<string> str;
*str = 'a';
std::shared_ptr<A> a;
*a = 'a'; // err: E0349 no operator "=" matches these operands
}
If I write class like this, then the code above compiles and runs:
struct A : string
{
using string::string;
using string::operator=;
};
CodePudding user response:
All member functions, that includes operator overloads, are inheritted following the regular inheritance rules for member functions.
Like @sklott mentioned, you need to implicitly pull in functions from the base class if they are shadowed in the derived (as in same name, but different arguments, etc).
In your case, the default assignment operator of A
shadows the assignment operators of string
. Therefore those must be explicitly injected via using
.