Get type of the parameter, templates, C++

前端 未结 2 1751
星月不相逢
星月不相逢 2021-02-20 00:41

There is the following simplified data structure:

Object1.h

template 
class Object1
{
  private:
     T a1;
     T a2;
  public:
     T g         


        
相关标签:
2条回答
  • 2021-02-20 00:48

    Add a typedef :

    template <class T>
    class Object1
    {
      private:
         T a1;
         T a2;
      public:
         T getA1() {return a1;}
         typedef T type;
    };
    
    template <class Object>
    void foo(Object *o1, Object *o2)
    {
       typename Object::type x = o1.getA1();
       ...
    }
    
    0 讨论(0)
  • 2021-02-20 01:09

    You can use this:

    template <template<class> class Object, class T>
    void func1(Object<T> &o1, Object<T> &o2)
    {
       T x = o1.getA1();
    }
    

    Working example at http://www.ideone.com/t8KON .

    Btw. if you use pointers as parameters, you have to use -> operator to call methods.

    0 讨论(0)
提交回复
热议问题