Как реализовать подобное?
От: DrMom  
Дата: 26.07.05 10:44
Оценка:
Сабж:

template<class T, class TDerived>
struct SObj
{
    typedef typename TDerived::Type Type;

    Type a;

    void Incr()
    {
        a++;
    }
};

template<class T>
struct SDerivedObj : public SObj<T, SDerivedObj<T> >
{
    typedef int Type;
};


Если нельзя так, то как можно? Интересны варианты VC6-7. Поможите чем сумеете.
Re: Как реализовать подобное?
От: A. Fedotov Украина  
Дата: 26.07.05 11:30
Оценка:
Здравствуйте, DrMom, Вы писали:

может подойдёт
при таких записях
typedef typename TDerived::Type Type;
....
typedef typename BaseType<T>::Type Type;

в SObj и SDerivedObj Type будет одним типом
template <typename T>
struct BaseType
{
typedef int Type;
};


template<class T, class TDerived>
struct SObj
{
    typedef typename TDerived::Type Type;

    Type a;

    void Incr()
    {
        a++;
    }
};


template<class T>
struct SDerivedObj : public SObj<T, BaseType<T> >
{
    typedef typename BaseType<T>::Type Type;
};


int _tmain(int argc, _TCHAR* argv[])
{
    SDerivedObj<int> l;
    return 0;
}
Re[2]: Как реализовать подобное?
От: DrMom  
Дата: 26.07.05 11:45
Оценка:
Здравствуйте, A. Fedotov, Вы писали:

AF>Здравствуйте, DrMom, Вы писали:


AF>может подойдёт

AF>при таких записях
AF>
AF>typedef typename TDerived::Type Type;
AF>....
AF>typedef typename BaseType<T>::Type Type;
AF>

AF>в SObj и SDerivedObj Type будет одним типом
AF>
AF>template <typename T>
AF>struct BaseType
AF>{
AF>typedef int Type;
AF>};


AF>template<class T, class TDerived>
AF>struct SObj
AF>{
AF>    typedef typename TDerived::Type Type;

AF>    Type a;

AF>    void Incr()
AF>    {
AF>        a++;
AF>    }
AF>};


AF>template<class T>
AF>struct SDerivedObj : public SObj<T, BaseType<T> >
AF>{
AF>    typedef typename BaseType<T>::Type Type;
AF>};


AF>int _tmain(int argc, _TCHAR* argv[])
AF>{
AF>    SDerivedObj<int> l;
AF>    return 0;
AF>}
AF>


Спасибо большое. Я только чуток подправил кое-что:


template <typename T>
struct BaseType
{
typedef int Type;
};


template<class T, class TDerived>
struct SObj
{
    typedef typename TDerived::Type Type;

    Type a;

    void Incr()
    {
        a++;
    }
};


template<class T>
struct SDerivedObj : public BaseType<T>, public SObj<T, BaseType<T> >
{
};
Re[2]: Как реализовать подобное?
От: MaximE Великобритания  
Дата: 26.07.05 13:12
Оценка: 1 (1) +1
A. Fedotov wrote:

> Здравствуйте, DrMom, Вы писали:

>
> может подойдёт
> при таких записях
>
> typedef typename TDerived::Type Type;
> ....
> typedef typename BaseType<T>::Type Type;
>

> в SObj и SDerivedObj Type будет одним типом
>
> template <typename T>
> struct BaseType
> {
> typedef int Type;
> };
>
>
> template<class T, class TDerived>
> struct SObj
> {
>     typedef typename TDerived::Type Type;
>
>     Type a;
>
>     void Incr()
>     {
>         a++;
>     }
> };
>
>
> template<class T>
> struct SDerivedObj : public SObj<T, BaseType<T> >
> {
>     typedef typename BaseType<T>::Type Type;
> };
>
>
> int _tmain(int argc, _TCHAR* argv[])
> {
>     SDerivedObj<int> l;
>     return 0;
> }
>


Нужно получить типы, ассоциированные с типом. Наследование здесь не обязательно. На мой взгляд, здесь больше подходят traits:

template<class T>
struct SObj
{
     typedef typename T::Type Type;

     Type a;

     void Incr()
     {
         a++;
     }
};


template<class T>
struct SDerivedObj;

template <typename T>
struct MyTraits
{
     typedef SDerivedObj<T> Derived;
     typedef T Type;
}

template<class T>
struct SDerivedObj : public SObj<MyTraits<T> >
{
};


--
Maxim Yegorushkin
Posted via RSDN NNTP Server 1.9
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.