Accessing a protected member of a class - Programmer

This is a discussion on Accessing a protected member of a class - Programmer ; Hierarchy I ----------- class CBaseForAllScreens class CCMCFormView : public CFormView class CBaseFormView : public CCMCFormView, public CBaseForAllScreens class CBaseExpandableView : public CBaseFormView Hierarchy II ------------ class CBaseSingleView : public CBaseFormView class CHiRDBNicknameList : public CBaseSingleView MyClass inherits from CHiRDBNicknameList I ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Accessing a protected member of a class

  1. Accessing a protected member of a class

    Hierarchy I
    -----------
    class CBaseForAllScreens
    class CCMCFormView : public CFormView
    class CBaseFormView : public CCMCFormView, public CBaseForAllScreens
    class CBaseExpandableView : public CBaseFormView

    Hierarchy II
    ------------
    class CBaseSingleView : public CBaseFormView
    class CHiRDBNicknameList : public CBaseSingleView


    MyClass inherits from CHiRDBNicknameList
    I want to access a protected member function of the
    CBaseExpandableView class.


    Two ways in which this can be achieved is
    1. Deriving MyClass from CBaseExpandableView
    2. Declaring MyClass as a friend of CBaseExpandableView

    Problems:
    a. Using inheritance.
    Since both CBaseSingleView and CBaseExpandableView inherit from
    CBaseFormView, I derive both of them virtually from CBaseFormView.
    Doing so gives me errors like
    error C2512: 'CBaseFormView::CBaseFormView' : no appropriate default
    constructor available
    as none of the base classes hava default constructors.
    To resolve this I need to explicitly specify
    CBaseFormView(CAddHiRDB::IDD) as in
    CAddHiRDB::CAddHiRDB():CBaseSingleView(CAddHiRDB:: IDD),
    CBaseFormView(CAddHiRDB::IDD)

    There are too many clases like CAddHiRDB where I would have to make
    changes which is not feasible.

    b. Using friend
    I make MyClass a friend of CBaseExpandableView
    To access the protected member I declared a static CBaseExpandableView
    xx
    but since the base classes do not have a default constructor I have to
    initialize xx.
    The constructors of the base class are single argument constructors
    that pass the resource ID to CFormView(MFC class). Since there is no
    resouce I cannot pass an ID. The protected fn doesn't require any such
    id and can be used independently. Even if I pass a valid resource id
    while initializing xx in the .cpp file I get an "Access violation"
    while executing this code.
    CBaseExpandableView CBaseExpandableView::xx(CUpdateCMDB::IDD);

    Qs:
    1. Is there a mechanism other than the inheritance and friend classes
    to access this protected member function ??
    2. Any possible solution to the above problem??

  2. Re: Accessing a protected member of a class

    do the friend thing, and then do a cast when you want to access the
    protected function. Do not create any instances of the new class. This
    limits the effect of the hack (which this most definately is) to the desired
    result.

    class CHackedView : public CBaseExpandableView
    {
    friend class CMyClass;

    ...or better yet...

    friend CMyClass::MyFunction;
    };


    ....
    ((CHackedView*)pBaseExpandableView)->SomeProtectedFunction();
    ....

    "mshetty" wrote in message
    news:bfbb8fd4.0310020457.311d5c1f@posting.google.c om...
    > Hierarchy I
    > -----------
    > class CBaseForAllScreens
    > class CCMCFormView : public CFormView
    > class CBaseFormView : public CCMCFormView, public CBaseForAllScreens
    > class CBaseExpandableView : public CBaseFormView
    >
    > Hierarchy II
    > ------------
    > class CBaseSingleView : public CBaseFormView
    > class CHiRDBNicknameList : public CBaseSingleView
    >
    >
    > MyClass inherits from CHiRDBNicknameList
    > I want to access a protected member function of the
    > CBaseExpandableView class.
    >
    >
    > Two ways in which this can be achieved is
    > 1. Deriving MyClass from CBaseExpandableView
    > 2. Declaring MyClass as a friend of CBaseExpandableView
    >
    > Problems:
    > a. Using inheritance.
    > Since both CBaseSingleView and CBaseExpandableView inherit from
    > CBaseFormView, I derive both of them virtually from CBaseFormView.
    > Doing so gives me errors like
    > error C2512: 'CBaseFormView::CBaseFormView' : no appropriate default
    > constructor available
    > as none of the base classes hava default constructors.
    > To resolve this I need to explicitly specify
    > CBaseFormView(CAddHiRDB::IDD) as in
    > CAddHiRDB::CAddHiRDB():CBaseSingleView(CAddHiRDB:: IDD),
    > CBaseFormView(CAddHiRDB::IDD)
    >
    > There are too many clases like CAddHiRDB where I would have to make
    > changes which is not feasible.
    >
    > b. Using friend
    > I make MyClass a friend of CBaseExpandableView
    > To access the protected member I declared a static CBaseExpandableView
    > xx
    > but since the base classes do not have a default constructor I have to
    > initialize xx.
    > The constructors of the base class are single argument constructors
    > that pass the resource ID to CFormView(MFC class). Since there is no
    > resouce I cannot pass an ID. The protected fn doesn't require any such
    > id and can be used independently. Even if I pass a valid resource id
    > while initializing xx in the .cpp file I get an "Access violation"
    > while executing this code.
    > CBaseExpandableView CBaseExpandableView::xx(CUpdateCMDB::IDD);
    >
    > Qs:
    > 1. Is there a mechanism other than the inheritance and friend classes
    > to access this protected member function ??
    > 2. Any possible solution to the above problem??




+ Reply to Thread