I am currently suffering a brain fart. I've done this before but I can't remember the exact syntax and I can't look at the code I wrote because I was working at another company at the time. I have this arrangement:
class P
{
// stuff
};
class PW : public P
{
// more stuff
};
class PR : public P
{
// more stuff
};
class C
{
public:
P GetP() const { return p; }
private:
P p;
};
// ...
P p = c.GetP( ); // valid
PW p = c.GetP( ); // invalid
PR p = c.GetP( ); // invalid
// ...
Now I would like to make P interchangeable with PW and PR (and thus PW and PR can be interchanged). I could probably get away with casts but this code change has occurred quite a few times in this module alone. I am pretty sure it is a operator but for the life of me I can't remember what.
How do I make P interchangeable with PW and PR with minimal amount of code?
Update: To give a bit more clarification. P stands for Project and the R and W stands for Reader and Writer respectively. All the Reader has is the code for loading - no variables, and the writer has code for simply Writing. It needs to be separate because the Reading and Writing sections has various manager classes and dialogs which is none of Projects real concern which is the manipulation of project files.
Update: I also need to be able to call the methods of P and PW. So if P has a method a() and PW as a method call b() then I could :
PW p = c.GetP();
p.a();
p.b();
It's basically to make the conversion transparent.