小编MMS*_*MMS的帖子

C++,非模板类中的模板变量

我使用以下模板类:

template <class T>
class Point2D
{
    private:
            T x;
            T y;
...
};

template <class T>
class Point2D;

template <class T>
class Line{
    private:
          Point2D <T> *start;  
          Point2D <T> *start; 
    .... 
};
Run Code Online (Sandbox Code Playgroud)

如果我想创建一个对象Line,则必须写出Line的点和类型的类型

int main
{
     Point2DC<double> p1(0,0);
     Point2DC<double> p2(10,10);
     Line<double> l(&p1,&p2);
     ...
}
Run Code Online (Sandbox Code Playgroud)

我发现它很无意义......如果点数是双倍的,那么Line也必须加倍......是否有可能只在类Line中指出一些指针并且不要对所有类进行模板化,就像那样

template <class T>
class Point2D;

class Line{
    private:
          template <class T>
          Point2D <T> *start;  
          Point2D <T> *start; 
    .... 
};
Run Code Online (Sandbox Code Playgroud)

并使用

int main
{
     Point2D<double> p1(0,0);
     Point2D<double> p2(10,10);
     Line l(&p1,&p2);
     ...
}
Run Code Online (Sandbox Code Playgroud)

c++ templates

4
推荐指数
1
解决办法
1359
查看次数

C++模板+ typedef

以下代码有什么问题:

Point2D.h

template <class T> 
class Point2D 
{     
   private:
         T x;
         T y; 
   ... 
 };
Run Code Online (Sandbox Code Playgroud)

PointsList.h

template <class T>
class Point2D;

template <class T>
struct TPointsList
{
    typedef std::vector <Point2D <T> > Type;
};

template <class T>
class PointsList
{
    private:
            TPointsList <T>::Type points;  //Compiler error
 ...
};
Run Code Online (Sandbox Code Playgroud)

我想创建没有直接类型规范的新用户类型TPointsList ...

c++ templates typedef

0
推荐指数
1
解决办法
1356
查看次数

标签 统计

c++ ×2

templates ×2

typedef ×1