template class question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • girays

    template class question

    Hello everyone,

    I have a simple problem what I don't know the exact syntac for this.
    My code is shown below:

    enum LengthType
    {
    LENGTH_METER = 0,
    LENGTH_KMETER,
    LENGTH_YARD, // 1 yard = 0,914 meter
    LENGTH_KYARD
    };

    template <LengthType L>
    class Length
    {
    public:
    Length(void);
    ~Length(void);

    // Length<toTypeco nvertTo(LengthT ype toType) const; ???????

    private:
    LengthType m_type;
    float m_value;
    };

    template <LengthType L>
    Length<L>::Leng th( void )
    : m_type(L)
    , m_value()
    {
    }


    You can see the lined marked ??????? at the end of line. I want to
    convert from one Length object to another Length object. How can I
    write this simple (may be not simple but I don't know how to do with
    template code) code?

    Thanks, regards ...
  • Gianni Mariani

    #2
    Re: template class question

    girays wrote:
    Hello everyone,
    >
    I have a simple problem what I don't know the exact syntac for this.
    My code is shown below:
    >
    enum LengthType
    {
    LENGTH_METER = 0,
    LENGTH_KMETER,
    LENGTH_YARD, // 1 yard = 0,914 meter
    LENGTH_KYARD
    };
    >
    template <LengthType L>
    class Length
    {
    public:
    Length(void);
    ~Length(void);
    >
    // Length<toTypeco nvertTo(LengthT ype toType) const; ???????
    >
    private:
    LengthType m_type;
    float m_value;
    };
    >
    template <LengthType L>
    Length<L>::Leng th( void )
    : m_type(L)
    , m_value()
    {
    }
    >
    >
    You can see the lined marked ??????? at the end of line. I want to
    convert from one Length object to another Length object. How can I
    write this simple (may be not simple but I don't know how to do with
    template code) code?
    Show me how you would use the code.


    e.g.

    Length<YARDyard s(3);
    Length<METREmet res(yards);

    But then, these are all the same units, so why would you just not do the
    conversion at the point of constructing the object so you have a
    homogeneous type.

    e.g.

    Length len(YARD,3);

    There are a number of physical unit libraries, I've not used them
    myself, but they can do things like:

    Physical<Length ,doublelen(YARD ,5);
    Physical<Force, doubleforce(NEW TON,5);

    Physical<Combin e<Length,Force> ::Unit, floatmoment( len * force );

    In your case, it seems like you don't need any templates since you can
    allways store your length in a normalized form.

    Comment

    • Barry

      #3
      Re: template class question

      On Jun 1, 5:56 am, girays <selcukgi...@gm ail.comwrote:
      Hello everyone,
      >
      I have a simple problem what I don't know the exact syntac for this.
      My code is shown below:
      >
              enum LengthType
              {
                      LENGTH_METER = 0,
                      LENGTH_KMETER,
                      LENGTH_YARD,    // 1 yard = 0,914 meter
                      LENGTH_KYARD
              };

      add conversion ratio template
      template <LengthType fromType, LengthType toType>
      struct ConvRatio;

      template <>
      struct ConvRatio <LENGTH_METER , LENGTH_KMETER>
      { enum { value = 1000 }; };
      // if value is not always integer,
      // use a static member function to return a float

      // other conversion ratios go here ...
      >
              template <LengthType L>
              class Length
              {
              public:
                      Length(void);
                      ~Length(void);
      >
                      // Length<toTypeco nvertTo(LengthT ype toType) const; ???????

      template <LengthType toType>
      Length<toTypeco nvertTo() const
      { return m_value * ConvRatio<L, toType>::value; }
      >
              private:
                      LengthType m_type;
                      float m_value;
              };
      >
              template <LengthType L>
                      Length<L>::Leng th( void )
                      : m_type(L)
                      , m_value()
              {
              }
      >
      You can see the lined marked ??????? at the end of line. I want to
      convert from one Length object to another Length object. How can I
      write this simple (may be not simple but I don't know how to do with
      template code) code?
      >

      Suppose you have constructor
      Length::Length( float);
      and define your Length destruction;

      Here is a test case:

      int main()
      {
      Length<LENGTH_M ETERlen1(15);
      Length<LENGTH_K METERlen2 = len1.convertTo< LENGTH_KMETER>( );
      //std::cout << len2 << std::endl; // need to friend the ostream
      inserter
      }

      --
      Best Ragards
      Barry

      Comment

      • Vincent Jacques

        #4
        Re: template class question

        Hello,

        girays a écrit :
        // Length<toTypeco nvertTo(LengthT ype toType) const; ???????
        I would say that your main problem here is that you seem to want a
        different return type according to the *value* of your parameter. There
        is no direct construct for this in C++.

        You will have to choose between two approaches:

        1) Units are static: in this case, values of type LengthType will always
        appear are template argument, either of your class Length, or of the
        member function convertTo. Note that your class Length does not need a
        member of type LengthType.

        2) Units are dynamic: there will be no template at all. You will have a
        member variable of type LengthType in class Length. There will be a
        parameter of type LengthType in the constructor of Length and in the
        function convertTo.

        I hope it helps,
        --
        Vincent Jacques

        "S'il n'y a pas de solution, c'est qu'il n'y a pas de problème"
        Devise Shadock

        Comment

        • girays

          #5
          Re: template class question

          On Jun 1, 2:43 am, Gianni Mariani <gi4nos...@mari ani.wswrote:
          girays wrote:
          Hello everyone,
          >
          I have a simple problem what I don't know the exact syntac for this.
          My code is shown below:
          >
          enum LengthType
          {
          LENGTH_METER = 0,
          LENGTH_KMETER,
          LENGTH_YARD, // 1 yard = 0,914 meter
          LENGTH_KYARD
          };
          >
          template <LengthType L>
          class Length
          {
          public:
          Length(void);
          ~Length(void);
          >
          // Length<toTypeco nvertTo(LengthT ype toType) const; ???????
          >
          private:
          LengthType m_type;
          float m_value;
          };
          >
          template <LengthType L>
          Length<L>::Leng th( void )
          : m_type(L)
          , m_value()
          {
          }
          >
          You can see the lined marked ??????? at the end of line. I want to
          convert from one Length object to another Length object. How can I
          write this simple (may be not simple but I don't know how to do with
          template code) code?
          >
          Show me how you would use the code.
          >
          e.g.
          >
          Length<YARDyard s(3);
          Length<METREmet res(yards);
          >
          But then, these are all the same units, so why would you just not do the
          conversion at the point of constructing the object so you have a
          homogeneous type.
          >
          e.g.
          >
          Length len(YARD,3);
          >
          There are a number of physical unit libraries, I've not used them
          myself, but they can do things like:
          >
          Physical<Length ,doublelen(YARD ,5);
          Physical<Force, doubleforce(NEW TON,5);
          >
          Physical<Combin e<Length,Force> ::Unit, floatmoment( len * force );
          >
          In your case, it seems like you don't need any templates since you can
          allways store your length in a normalized form.
          That's exactly what I want to do Gianni. the example is gave matches
          my needs.
          It's easy to do without templates, but I'm actually looking for a
          template solution.

          Comment

          Working...