Thread: Acessing inherited fields?

  1. #1
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102

    Acessing inherited fields?

    The below scenarios are simplifications of some stuff I'm doing with template classes, and I have some questions.

    Scenario 1:

    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;
    };
    #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.


    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?
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >#1.
    Friendship is not inherited.

    >Scenario 2, what if I change the keyword private to protected?
    Same deal, friendship is not inherited.

    >Scenario 3, the only way I've found that works, but not what I'm looking for
    That's really the only way you'll get what you want unless you make DerivedData a friend of BaseDataIterator as well.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Scenario 2, what if I change the keyword private to protected?
    > Same deal, friendship is not inherited.
    In this case that doesn't matter... see working example below:
    Code:
    #include <iostream>
    
    class BaseData
    {
    };
    
    class BaseDataIterator
    {
        friend class BaseData;
    public:
        BaseDataIterator() : i(25) { }
    protected:
        int i;
    };
    
    class DerivedDataIterator : public BaseDataIterator
    {
        friend class DerivedData;
    };
    
    class DerivedData:BaseData
    {
    public:
        void DoSomething(DerivedDataIterator & iter)
        {
            std::cout << iter.i << std::endl;
        }
    };
    
    int main()
    {
        DerivedDataIterator di;
        DerivedData d;
        d.DoSomething(di);
    }

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >In this case that doesn't matter... see working example below:
    I confused too, because I tried the OP's code with protected instead of private and it appears to compile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit fields
    By Perspektyva in forum C++ Programming
    Replies: 13
    Last Post: 11-22-2008, 02:38 PM
  2. Lists: adding fields (not nodes) & more
    By Mariano L Gappa in forum C++ Programming
    Replies: 15
    Last Post: 11-09-2005, 07:26 PM
  3. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  4. strings & fields
    By marko in forum C Programming
    Replies: 10
    Last Post: 05-01-2003, 01:42 PM