Hello I need to access data from DataContainer to Derived by Base class. I can't just make this data public because I use this class in place where it shouldn't be accessed.
I could use just friend but then I have to make accessors for Derived classes in the Base class. This will make it inextensible.
#include <vector>
class Data; // It's not important
class DataContainer
{
protected:
std::vector<Data> dataVector;
std::vector<Data> dataVector2;
};
class Base
{
protected:
DataContainer* dataContainer;
public:
virtual ~Base() {};
void SetDataContainer(DataContainer* dataContainer)
{
this->dataContainer = dataContainer;
}
virtual void UseDataFromVector() = 0;
};
class Derived:public Base
{
public:
virtual ~Derived() {};
virtual void UseDataFromVector()
{
//And here want to use data from DataContainer...
}
};
My question is how to access this data without making it public or friend.
UPDATE
This answer doesn't help me because friend does not hurt me. I just try to avoid writing lines of code by smart move. I could just write something like this:
class DataContainer
{
friend class Base
std::vector<Data> dataVector;
};
class Base
{
DataContainer* dataContainer;
std::vector<Data>& GetDataVector() { return dataContainer->dataVector;}
};
But when I add more vectors in DataContainer I'll have to update this class.