Hello,
I have a chain of inherited classes like this:
class A
{
protected:
int a;
public:
int getA(){return a;}
};
class B:public A
{
protected:
int b;
public:
int getB(){return b;}
};
Based on that you can see that the public can only read the values in those classes, they cannot write them. However I would like to make a sort of overriding class that will allow those with access to it to modify the classes, like so:
class ExposedA:public A
{
public:
ExposedA &setA(int a){this->a=a;return *this;}
};
//here is where I am lost
class ExposedB:public B
{
public:
ExposedB &setB(int b){this->b=b;return *this;}
};
I would like to be able to also modify the a part of B, just as if I swapped ExposedA into the B class's inheritance instead of A. I could of course just inherit ExposedB from ExposedA, however I would like to be able to cast my ExposedB pointers to B pointers so that I could send them out in their unmodifyable state. Is this at all possible? (I looked into virtual access specifiers, they got me a bit confused but it seems like they could help?)
Basically this is my goal:
ExposedB *eb;
eb->setB(1)->setA(2);
B *safeB=(B*)eb;
int a=safeB->getA();
int b=safeB->getB();
//I want the following two lines to fail:
safeB->setA(1);
safeB->setB(2);