AutosarC++17_10-A10.3.5ΒΆ
A user-defined assignment operator shall not be virtual
Required inputs: IR
Bad code (virtual assignment operator):
class Base {
public:
virtual Base& operator=(const Base& other) { // ERROR: virtual
return *this;
}
};
class Derived1 : public Base {
public:
Derived1& operator=(const Base& other) override;
};
class Derived2 : public Base {
public:
Derived2& operator=(const Base& other) override;
};
void Use() {
Derived1 d1;
Derived2 d2;
d1 = d2; // Calls Derived1::operator=(const Base&)
}
Good code (non-virtual assignment operator):
class Base {
public:
Base& operator=(const Base& other) { // OK: non-virtual
// Base assignment logic
return *this;
}
};
class Derived : public Base {
public:
Derived& operator=(const Derived& other) { // OK: different signature
Base::operator=(other);
// Derived-specific assignment logic
return *this;
}
};
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
assignment_virtual |
A user-defined assignment operator shall not be virtual. |
None |
False |
Options
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.