CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2001
    Posts
    2,455

    Inheritance Question

    I am getting an error I did not expect:
    C:\Development\Inform2K4\TaskPlanPage.cpp(91) : error C2248: 'SetBoundary' : cannot access protected member declared in class 'CTaskPlanPageObject'
    Code:
    class CTaskPlanPageObject
    {
           CTaskPlanPageObject();
           virtual ~CTaskPlanPageObject();
    protected:
           virtual void SetBoundary(const CRect& rcBoundary);
    protected:
           CRect m_rcBoundary;
    };
    
    //.cpp
    void CTaskPlanPageObject::SetBoundary(const CRect& rcBoundary)
    {
            m_rcBoundary = rcBoundary;
    }
    
    class CTaskPlanPageHeader : public CTaskPlanPageObject
    {
             CTaskPlanPageHeader();
             virtual ~CTaskPlanPageHeader();
    };
    Now, if I have a CTaskPlanPageHeader object, shouldn't calling SetBoundary() be OK? Or do I not understand inheritance?

    Code:
    void CTaskPlanPage::SetPageSize(const CSize& szPage)
    {
            .....
            m_Header.SetBoundary(rcBoundary);
    }
    Mike B
    Last edited by cilu; July 6th, 2006 at 12:46 PM. Reason: fixed code tags

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Inheritance Question

    CTaskPlanPage isn't in the hierarchy you posted.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Inheritance Question

    No, you can't do that. You can call SetBoundary() from within a member function of CTaskPlanPageHeader, but not from another class CTaskPlanPage.

    I hope this can be helpful:
    Code:
    class base
    {
    private:
    	virtual void f() {}
    protected:
    	virtual void g() {}
    public:
    	virtual void h() {}
    };
    
    class derived : public base
    {
    protected:
    	void any()
    	{
    		f();	// not ok
    		g();	// ok
    		h();	// ok
    	}
    };
    
    class derived1 : public base
    {
    public:
    	// f() is now made public
    	virtual void f() {}
    };
    
    
    int main()
    {
    	derived d;
    	d.f();	// not ok
    	d.g();	// not ok
    	d.h();	// ok
    
    	derived1 d1;
    	d1.f();	// ok
    
    	return 0;
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Feb 2001
    Posts
    2,455

    Re: Inheritance Question

    Quote Originally Posted by GCDEF
    CTaskPlanPage isn't in the hierarchy you posted.
    Sorry,
    Code:
    class CTaskPlanPageObject
    {
           CTaskPlanPageObject();
           virtual ~CTaskPlanPageObject();
    protected:
           virtual void SetBoundary(const CRect& rcBoundary);
    protected:
           CRect m_rcBoundary;
    };
    
    //.cpp
    void CTaskPlanPageObject::SetBoundary(const CRect& rcBoundary)
    {
            m_rcBoundary = rcBoundary;
    }
    
    class CTaskPlanPageHeader : public CTaskPlanPageObject
    {
             CTaskPlanPageHeader();
             virtual ~CTaskPlanPageHeader();
    };
    
    class CTaskPlanPage : CTaskPlanPageObject
    {
    public:
           CTaskPlanPage();
           virtual ~CTaskPlanPage();
    
    private:
           CTaskPlanPageHeader m_Header;
    };
    Note I derived the CTaskPlanPage from CTaskPlanPageObject as well since the page and all objects on that page share some functionality such as SetBoundary(...).

    cilu, I am not sure I understand. If I derive a class from CListCtrl for example, I can still call the base class (CListCtrl) InsertColumn(....) function
    from within a view.

    e.g:
    Code:
    class CMyListCtrl : CListCtrl
    {
    public:
            CMyListCtrl();
            virtual ~CMyListCtrl();
    };
    
    class CMyDialog : CDialog
    {
           CListCtrl	m_cComponents;
    };
    
    void CMyDialog::OnInitDialog()
    {
           m_cComponents.InsertColumn(....);
    }
    Wouldn't this be basically the same thing?

    Mike B

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Inheritance Question

    Mike, InsertColumn is PUBLIC. Your SetBoundary is PROTECTED. Does that tell you anything?

    Quote Originally Posted by MSDN
    private
    Class members declared as private can be used only by member functions and friends (classes or functions) of the class.

    protected
    Class members declared as protected can be used by member functions and friends (classes or functions) of the class. Additionally, they can be used by classes derived from the class.

    public
    Class members declared as public can be used by any function.
    The problem is that you are calling SetBoundary from a derived class, but not for the current instance (this) but for another object. That doesn't work, because it's not allowed by the definition of the protected accessing rights.

    To complete my example, this is exactly what you are doing...
    Code:
    class derived : public base
    {
    protected:
    	void any()
    	{
    		base b;
    		b.f();	// not ok
    
    		f();	// not ok
    		g();	// ok
    		h();	// ok
    	}
    };
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Inheritance Question

    You're calling it through CTaskPlanPageHeader, which doesn't have a direct relationship to CTaskPlanPage.

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Inheritance Question

    Please take a look on this MSDN page for more.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Feb 2001
    Posts
    2,455

    Re: Inheritance Question

    Ahhh found it, sorry for wasitng your time.
    I forgot to include public in the class declaration:
    Code:
    class CTaskPlanPageHeader : public CTaskPlanPageObject
    Code:
    
    
    Mike B
    Last edited by MikeB; July 6th, 2006 at 01:24 PM.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Inheritance Question

    Quote Originally Posted by MikeB
    [B]
    Now I left out the ctors/dtors for clarity. Also, I called SetBoundary(...) from a CTaskPlanPage object in CTaskPlan::OnInitialUpdate() to test. This compiles fine. But when I call SetBoundary() from the CTaskPlanPageHeader object, the compiler tells me it is invalid. It is public inheritance.

    Mike B
    What exactly does the compiler say?

  10. #10
    Join Date
    Feb 2001
    Posts
    2,455

    Re: Inheritance Question

    Quote Originally Posted by GCDEF
    What exactly does the compiler say?
    I found the problem, see above.

    The compiler just said,
    C:\Development\Inform2K4\TaskPlanPage.cpp(91) : error C2248: 'SetBoundary' : cannot access public member declared in class 'CTaskPlanPageObject'
    C:\Development\Inform2K4\TaskPlanPageObject.h(22) : see declaration of 'SetBoundary'
    Mike B

  11. #11
    Join Date
    Feb 2002
    Posts
    5,757

    Re: Inheritance Question

    // CTaskPlanPageHeader
    public
    virtual void SetBoundary(CRect const &rcBoundary)
    {
    CTaskPlanPageObject::SetBoundary(rcBoundary)
    }

    Kuphryn

  12. #12
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Inheritance Question

    Quote Originally Posted by MikeB
    Ahhh found it, sorry for wasitng your time.
    I forgot to include public in the class declaration:
    Code:
    class CTaskPlanPageHeader : public CTaskPlanPageObject
    Code:
    
    
    Mike B
    Right. IIRC, default inheritance between classes is private. One of the restrictions of private inheritance is that derrived objects CANNOT access protected or private members of base classes.

    I'm kinda fuzzy on exactly what situations warrent protected and private inheritance (I've never really needed it). But there have been many discussions on this forum about this.

    Viggy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured