I see. In which case then just adapt the constructor of the Subject class so it expects a pointer-to-Teacher.
I would send this code back, though, if I were code reviewing it. You're creating a Teacher object with
new
and giving it straight to the Subject class, which means that unless the Subject class calls
delete
on that pointer when the Subject class is destroyed, you'll get a memory leak.
However, if the Subject class
does call delete, what happens if someone creates one like this:
1 2 3 4 5 6
|
int main()
{
Teacher someTeacherObject{"Daniel","XFAC"};
{
Subject p{"Programming", &someTeacherObject};
} // CRASH here when the Subject object calls delete on something that wasn't allocated using new
| |
Sure, this may just be for a learning exercise, but that's no excuse to teach dangerous practices. Anyway, now you know.