Alex Rivera | Logout

Use of derived class' typedefs in base

Asked 2010-12-18T19:56:32.837
12

I want the derived class to be a descendant of a template class. That class depends on descendant's members. In short I want this code to compile:

struct IBootParams
{
    virtual bool GetImmediate() = 0;
};

template <class T>
struct TBootBootParams
{
    typename T::TransType transferType;

    typename T::UseAbort_ useAbort;

    bool GetImmediate()
    {
        if ( transferType == T::e1 )
        {
            return useAbort.someFlag;
        }

        return false;
    }

};

struct BootBootParams : public TBootBootParams<BootBootParams> 
{
    enum SomeEnum
    {
        e1=0,
        e2,
        e3
    };

    struct UseAbort
    {
        bool        someFlag;           

        char        someMember;             
        int         otherMember;        
    } useAbort;

    typedef SomeEnum TransType;
    typedef UseAbort UseAbort_;
};

struct BootAltBootParams : public TBootBootParams<BootAltBootParams> 
{
    enum SomeEnum
    {
        e1=5,
        e2,
        e3
    };

    struct UseAbort
    {
        bool        someFlag;           

        long long       someMember;             
        long long       otherMember;        
    } useAbort;

    typedef SomeEnum TransType;
    typedef UseAbort UseAbort_;
};

int _tmain(int argc, _TCHAR* argv[])
{
    BootBootParams bp;
    BootAltBootParams bpa;

    bool f = bp.GetImmediate();
    f = bpa.GetImmediate();
}
Edit
Report

2 Answers

1

I tried a little change to ybungalobill's solution counting my own needs. And this what I got

template<class T> 
struct TBootBootParams_traits;

template <class T>
struct TBootBootParams
{
    typedef TBootBootParams_traits<T> Traits;

    typename Traits::TransType transferType;
    typename Traits::UseAbort_ useAbort;

    bool GetImmediate()
    {
        if ( transferType == TBootBootParams_traits<T>::e1 )
        {
            return useAbort.someFlag;
        }

        return false;
    }
};

struct BootBootParams;
struct BootAltBootParams;

template<> 
struct TBootBootParams_traits<BootBootParams>
{
    enum SomeEnum
    {
        e1=5,
        e2,
        e3
    };

    struct UseAbort
    {
        bool        someFlag;           

        long long       someMember;             
        long long       otherMember;        
    };

    typedef SomeEnum TransType;
    typedef UseAbort UseAbort_;
};

template<> 
struct TBootBootParams_traits<BootAltBootParams>
{
    enum SomeEnum
    {
        e1=5,
        e2,
        e3
    };

    struct UseAbort
    {
        bool        someFlag;           

        int someMember;             
        float otherMember;        
    };

    typedef SomeEnum TransType;
    typedef UseAbort UseAbort_;
};

struct BootBootParams : 
    public TBootBootParams<BootBootParams>
{

};

struct BootAltBootParams : 
    public TBootBootParams<BootAltBootParams>
{

};

int main(void)
{
    BootBootParams bp;
    BootAltBootParams bpa;

    bool f = bp.GetImmediate();
    f = bpa.GetImmediate();
}

To summarize.. I use a template class as a must-have option of another class for this class placing all type data there. This just what I needed. Thanks again for the option!

answered 2010-12-20T10:56:06.823
0

For this approach to work, you have to include the template class object as a member of the parent class.

answered 2010-12-18T20:11:57.883

Your Answer