object orientated, classes, copy constructors, polymorphism, etc
im going to type all this out and explain what i know, plz chime in to correct me or to explain in a different way or in more detail as im still not sure about it all.
Code:
class CLocation
{
public:
double x;
double y;
};
Code:
class CInvader
{
// Other code
CInvader(CLocation position);
// Other code
};
Code:
CInvader::CInvader(CLocation position)
{
// Other code
this->x = position.x;
this->y = position.y;
};
then later we can write
Code:
CPosition pos;
pos.x=200;
pos.y=400;
CInvader anInvader(pos);
problem with the constructor is it can get get called when you imply that you want it
e.g
Code:
void Attack(CInvader inv)
{
// Blah blah
}
// Somewhere in rest of program
CPosition invader1pos;
invader1pos.x = 200;
invader1pos.y = 300;
CInvader invader1(invader1pos);
// and later…
Attack(invader1pos); // Typo
I am trying to attack an invader using the Attack(CInvader inv) function, but I have mis-typed.
invader1pos belongs to the CPosition class, but it can still be sent to the function, the compiler simply casts the paramater type to the class type.
in this case a CPosition was sent in error instead of a CInvader but was able to create 1 by casting. problem is the new CInvader runs out of scope sooner than a copy of the object would (not sure i get that bit, is it local within the attack function?)
Edit: I apologise for not fully using code tags first time...