Beispiel
#include
"BaseClass.h"
struct
ST
{
int
a;
char
c;
};
class
Derived : Base{
public
:
Derived
(int newx, int newy);
Derived
&
operator
=(const
Derived
& rhs);
private
:
int x;
int y;
ST myStructure;
};
Derived
&
Derived
::
operator
=(
const
Derived
& rightValue)
{
if
(this == &rightValue){
return
*
this
;
}
x = rightValue.x;
y = rightValue.y;
myStructure.a = rightValue.myStructure.a;
myStructure.c = rightValue.myStructure.c;
return
*
this
;
}
Lösung
Rufen Sie den Zuordnungsoperator der Basisklasse ebenfalls auf.
#include
"BaseClass.h"
struct
ST
{
int
a;
char
c;
};
class
Derived : Base{
public
:
Derived
(int newx, int newy);
Derived
&
operator
=(const
Derived
& rhs);
private
:
int x;
int y;
ST myStructure;
};
Derived
&
Derived
::
operator
=(
const
Derived
& rightValue)
{
if
(this == &rightValue){
return
*
this
;
}
static_cast
<Base&>(*
this
) = rightValue;
x = rightValue.x;
y = rightValue.y;
myStructure.a = rightValue.myStructure.a;
myStructure.c = rightValue.myStructure.c;
return
*
this
;
}