tags:

views:

55

answers:

3

I have the following situation:

I have a object of type MyClass, which has a method to cast itself to its base class. The class includes a typedef for its base class and a method to do the upcast.

template <class T, class B>
class BaseClass;

template <class T>
class NoAccess;

template <class T>
class MyClass : public BaseClass<T, NoAccess<T> >
{
  private:
    typedef BaseClass<T, NoAccess<T> > base;

  public:
    base &to_base();
};

I need to pass the result of a base call to a functor Operator:

template <class Y>
class Operator
{
   Operator(Y &x);
};

Operator<???> op(myobject.to_base());

Is there a easy way to fill the ??? provided that I do not want to use NoAccess?

+2  A: 

You can make the typedef public:

Operator<MyClass<type>::base> op(myobject.to_base());
Niki Yoshiuchi
+3  A: 

You can define a factory function to use type deduction:

template< class Y >
Operator<Y> create_operator( Y &y ) {
    return Operator<Y>( y );
}
Potatoswatter
+3  A: 

I see three possibilities:

  1. make MyClass<T,B>::base a public identifier

  2. change your operator and make the constructor a template using type-erasure (might be hard):
    class Operator { template<class Y> Operator(Y &x); }

  3. move the code creating the Operator object into its own function template:
    template<typename T> Operator<T> createOperator(const T& base)
    {return Operator<T>(base);}

sbi