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();
}
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.
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.
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).
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.
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;
};
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With