T
TheFerryman
Hi,
I keep running up against the same design problem. Let's say I have a
cat object like this:
//-----------------------------------------------------------------------------------------
class Cat
{
private:
Vision* m_pVision;
Memory* m_pMemory
public:
//update vision and add all visible cats to memory
void Update(){m_pVision->Update(this);}
const Memory* GetMemory()const{return &m_pMemory;}
};
//-----------------------------------------------------------------------------------------
The Vision class looks like this:
//-----------------------------------------------------------------------------------------
class Vision
{
//returns a vector of all other cats pCat can see
vector<Cat*> GetAllOtherAnimalsInView(Cat* pCat);
void Update(Cat* pCat)
{
pCat->GetMemory()->Add(GetAllOtherAnimalsInView(pCat));
}
};
//-----------------------------------------------------------------------------------------
This is where the problem occurs, because GetMemory() must be non
const for Vision::Update to use it. And of course, if I make it non
const then there's no reason to have it private in the first place.
An alternative would be for Cat to delegate the call to Memory::Add,
by having its own Add method like so:
void Cat::Add(vector<Cat*> cats){m_pMemory->Add(cats);}
but as classes become more complex the number of extra methods I'd
have to include may get silly and I'd be better off merging the two
classes together (ie. The Memory class merged into the Cat class)
I seem to be getting this design issue fairly regularly and -- shame
on me -- I just end up creating a const and a non const version of the
call to obtain a member instance... which seems to me like very poor
design.
How should I handle this problem?
Many thanks for any feedback.
I keep running up against the same design problem. Let's say I have a
cat object like this:
//-----------------------------------------------------------------------------------------
class Cat
{
private:
Vision* m_pVision;
Memory* m_pMemory
public:
//update vision and add all visible cats to memory
void Update(){m_pVision->Update(this);}
const Memory* GetMemory()const{return &m_pMemory;}
};
//-----------------------------------------------------------------------------------------
The Vision class looks like this:
//-----------------------------------------------------------------------------------------
class Vision
{
//returns a vector of all other cats pCat can see
vector<Cat*> GetAllOtherAnimalsInView(Cat* pCat);
void Update(Cat* pCat)
{
pCat->GetMemory()->Add(GetAllOtherAnimalsInView(pCat));
}
};
//-----------------------------------------------------------------------------------------
This is where the problem occurs, because GetMemory() must be non
const for Vision::Update to use it. And of course, if I make it non
const then there's no reason to have it private in the first place.
An alternative would be for Cat to delegate the call to Memory::Add,
by having its own Add method like so:
void Cat::Add(vector<Cat*> cats){m_pMemory->Add(cats);}
but as classes become more complex the number of extra methods I'd
have to include may get silly and I'd be better off merging the two
classes together (ie. The Memory class merged into the Cat class)
I seem to be getting this design issue fairly regularly and -- shame
on me -- I just end up creating a const and a non const version of the
call to obtain a member instance... which seems to me like very poor
design.
How should I handle this problem?
Many thanks for any feedback.