The below scenarios are simplifications of some stuff I'm doing with template classes, and I have some questions.
Scenario 1:
#1. Should I be able to access 'i' like this since DerivedData "is a" BaseData, BaseData is a friend of BaseDataIterator, and DerivedDataIterator "is a" BaseDataIterator? I don't think so, but just want to make sure there isn't a way.Code:class BaseData { }; class BaseDataIterator { friend class BaseData; private: int i; }; class DerivedDataIterator:BaseDataIterator; class DerivedData:BaseData { public: void DoSomething(DerivedDataIterator & iter) { iter.i;//(Question #1) } }; class DerivedDataIterator:BaseDataIterator { friend class DerivedData; };
Scenario 2, what if I change the keyword private to protected?
This means that DerivedDataIterator will have the field 'i' that is accessible by it, then shouldn't classes it declares as friends also be able to access the field? I think I need something more than just the iter.i though. . . like some sort of qualifier that specifies I'm trying to access i of the base class, through the iterator. Keep in mind I'm trying to access from DerivedData, through DerivedDataIterator. I tried to dynamic cast to BaseDataIterator and then access it, but I still had no luck. I may be doing it wrong.
Scenario 3, the only way I've found that works, but not what I'm looking for:
Take scenario 2, and add a function in DerivedDataIterator that basically returns a reference to 'i', so that DerivedData calls iter.GetI() to get a reference.
I would prefer not to have to change the base classes private to protected, but I don't think the friend status granted to BaseData is being granted to DerivedData. Any ideas?