範例
#include
"BaseClass.h"
struct
ST
{
int
a;
char
c;
};
class
Derived : Base{
public
:
Derived();
Derived(
const
Derived& toCopy);
private
:
int x;
int y;
ST myStructure;
};
Derived::Derived(
const
Derived& toCopy)
{
x =
toCopy
.x;
y =
toCopy
.y;
myStructure.a =
toCopy
.myStructure.a;
myStructure.c =
toCopy
.myStructure.c;
}
解決方案
同時呼叫基礎類別的複製建構子。
#include
"BaseClass.h"
struct
ST
{
int
a;
char
c;
};
class
Derived : Base{
public
:
Derived();
Derived(
const
Derived& toCopy);
private
:
int x;
int y;
ST myStructure;
};
Derived::Derived(
const
Derived& toCopy)
: Base(toCopy)
{
x =
toCopy
.x;
y =
toCopy
.y;
myStructure.a =
toCopy
.myStructure.a;
myStructure.c =
toCopy
.myStructure.c;
}