-
July 6th, 2006, 12:44 PM
#1
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
-
July 6th, 2006, 12:50 PM
#2
Re: Inheritance Question
CTaskPlanPage isn't in the hierarchy you posted.
-
July 6th, 2006, 12:50 PM
#3
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;
}
-
July 6th, 2006, 01:00 PM
#4
Re: Inheritance Question
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
-
July 6th, 2006, 01:07 PM
#5
Re: Inheritance Question
Mike, InsertColumn is PUBLIC. Your SetBoundary is PROTECTED. Does that tell you anything?
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
}
};
-
July 6th, 2006, 01:07 PM
#6
Re: Inheritance Question
You're calling it through CTaskPlanPageHeader, which doesn't have a direct relationship to CTaskPlanPage.
-
July 6th, 2006, 01:08 PM
#7
Re: Inheritance Question
Please take a look on this MSDN page for more.
-
July 6th, 2006, 01:17 PM
#8
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
Mike B
Last edited by MikeB; July 6th, 2006 at 01:24 PM.
-
July 6th, 2006, 01:24 PM
#9
Re: Inheritance Question
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?
-
July 6th, 2006, 01:26 PM
#10
Re: Inheritance Question
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
-
July 6th, 2006, 01:36 PM
#11
Re: Inheritance Question
// CTaskPlanPageHeader
public
virtual void SetBoundary(CRect const &rcBoundary)
{
CTaskPlanPageObject::SetBoundary(rcBoundary)
}
Kuphryn
-
July 6th, 2006, 03:19 PM
#12
Re: Inheritance Question
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|