Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of derived class' typedefs in base

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();
}
like image 840
Unicorn Avatar asked Dec 18 '10 19:12

Unicorn


People also ask

What is the purpose of using derived class?

Derived Class: A class that is created from an existing class. The derived class inherits all members and member functions of a base class. The derived class can have more functionality with respect to the Base class and can easily access the Base class. A Derived class is also called a child class or subclass.

How do you call a derived class method in base?

A virtual function is a member function of a base class that is overridden by a derived class. When you use a pointer or a reference to the base class to refer to a derived class object, you can call a virtual function for that object and have it run the derived class's version of the function.

Can a derived class be a friend of base?

Inheritance and Friendship in C++If a base class has a friend function, then the function doesn't become a friend of the derived class(es).

What does a base class have access to in its derived class?

A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class. Constructors, destructors and copy constructors of the base class.


2 Answers

You can't do it this way. When the compiler instantiates TBootBootParams<BootBootParams> it has not yet completely read the whole definition of BootBootParams so you can't access its members from inside the definition of TBootBootParams (member functions of TBootBootParams are an exception because they are instantiated later).

The usual solution is to have a traits class:

template<class T> struct TBootBootParams_traits;

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

    typename TBootBootParams_traits<T>::UseAbort_ useAbort;

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

        return false;
    }

};

struct BootBootParams;
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_;
};

struct BootBootParams : 
    public TBootBootParams<BootBootParams>, 
    public TBootBootParams_traits<BootBootParams>
{
    UseAbort_ useAbort;
};
like image 92
Yakov Galka Avatar answered Nov 03 '22 08:11

Yakov Galka


There is no way this can work that isn't a bit of a hack. So here is my attempt at a bit of a hack:

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

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

    typename T::UseAbort_ useAbort;

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

        return false;
    }

};

struct BootBootParams_types
{
    enum SomeEnum
    {
        e1=0,
        e2,
        e3
    };

    struct UseAbort
    {
        bool        someFlag;

        char        someMember;
        int         otherMember;
    } useAbort;

    typedef SomeEnum TransType;
    typedef UseAbort UseAbort_;
};

struct BootBootParams : public BootBootParams_types,
                        public TBootBootParams<BootBootParams_types>
{
};

struct BootAltBootParams_types
{
    enum SomeEnum
    {
        e1=5,
        e2,
        e3
    };

    struct UseAbort
    {
        bool        someFlag;

        long long       someMember;
        long long       otherMember;
    } useAbort;

    typedef SomeEnum TransType;
    typedef UseAbort UseAbort_;
};

struct BootAltBootParams : public BootAltBootParams_types,
                           public TBootBootParams<BootAltBootParams_types>
{
};

int main(int argc, const char * argv[])
{
    BootBootParams bp;
    BootAltBootParams bpa;

    bool f = bp.GetImmediate();
    f = bpa.GetImmediate();
}
like image 32
Omnifarious Avatar answered Nov 03 '22 07:11

Omnifarious