开发者

Get type of the parameter from list of objects, templates, C++

开发者 https://www.devze.com 2023-02-11 08:42 出处:网络
This question follows to my previous question Get type of the parameter, templates, C++ There is the following data structure:

This question follows to my previous question

Get type of the parameter, templates, C++

There is the following data structure:

Object1.h

template <cla开发者_JS百科ss T>
class Object1
{
  private:
     T a1;
     T a2;
  public:
     T getA1() {return a1;}
     typedef T type;
};

Object2.h

template <class T>
class Object2: public Object1 <T>
{
   private:
      T b1;
      T b2;
  public:
     T getB1() {return b1;}
}

List.h

template <typename Item>
struct TList
{
  typedef std::vector <Item> Type;
};

template <typename Item>
class List
{
  private: 
   typename TList <Item>::Type items;

  public:
   Item & operator [] ( const unsigned int index ) {return this->items[index];}
};

Is there any way how to get type T of an object from the list of objects (i.e. Object is not a direct parameter of the function but a template parameter)?

Process.h

class Process
{
  template <class Object>
  static void process (List <Object> *objects)
  {
    typename Object::type a1 = (*objects[0]).getA1(); // g++ error: 'Object1<double>*' is not a class, struct, or union type
  } 
};

But his construction works (i.e. Object represents a parameter of the function)

template <class Object>
void process (Object *o1) 
{
    typename Object::type a1 = (*o1).getA1(); // OK
}

There is the main program:

int main()
{
   Object1 <double> o1;
   Object1 <double> o1;

   List <Object1 <double> > list;

   Process::process(&list);

}


The problem is with objects[0], where objects is a pointer. You should write that as (*objects)[0] to call operator[] of the object pointed to.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号