template class question

Discussion in 'C++' started by girays, May 31, 2008.

  1. girays

    girays Guest

    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<toType> convertTo(LengthType toType) const; ???????

    private:
    LengthType m_type;
    float m_value;
    };

    template <LengthType L>
    Length<L>::Length( 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 ...
     
    girays, May 31, 2008
    #1
    1. Advertising

  2. 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<toType> convertTo(LengthType toType) const; ???????
    >
    > private:
    > LengthType m_type;
    > float m_value;
    > };
    >
    > template <LengthType L>
    > Length<L>::Length( 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<YARD> yards(3);
    Length<METRE> metres(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,double> len(YARD,5);
    Physical<Force,double> force(NEWTON,5);

    Physical<Combine<Length,Force>::Unit, float> moment( 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.
     
    Gianni Mariani, Jun 1, 2008
    #2
    1. Advertising

  3. girays

    Barry Guest

    On Jun 1, 5:56 am, 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
    >         };



    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<toType> convertTo(LengthType toType) const; ???????



    template <LengthType toType>
    Length<toType> convertTo() const
    { return m_value * ConvRatio<L, toType>::value; }

    >
    >         private:
    >                 LengthType m_type;
    >                 float m_value;
    >         };
    >
    >         template <LengthType L>
    >                 Length<L>::Length( 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_METER> len1(15);
    Length<LENGTH_KMETER> len2 = len1.convertTo<LENGTH_KMETER>();
    //std::cout << len2 << std::endl; // need to friend the ostream
    inserter
    }

    --
    Best Ragards
    Barry
     
    Barry, Jun 1, 2008
    #3
  4. Hello,

    girays a écrit :
    > // Length<toType> convertTo(LengthType 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
     
    Vincent Jacques, Jun 1, 2008
    #4
  5. girays

    girays Guest

    On Jun 1, 2:43 am, Gianni Mariani <> wrote:
    > 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<toType> convertTo(LengthType toType) const; ???????

    >
    > > private:
    > > LengthType m_type;
    > > float m_value;
    > > };

    >
    > > template <LengthType L>
    > > Length<L>::Length( 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<YARD> yards(3);
    > Length<METRE> metres(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,double> len(YARD,5);
    > Physical<Force,double> force(NEWTON,5);
    >
    > Physical<Combine<Length,Force>::Unit, float> moment( 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.
     
    girays, Jun 1, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    478
    Chris Theis
    Jul 24, 2003
  2. christopher diggins
    Replies:
    16
    Views:
    756
    Pete Becker
    May 4, 2005
  3. Replies:
    1
    Views:
    2,113
    Gianni Mariani
    Jun 8, 2007
  4. nguillot
    Replies:
    5
    Views:
    532
  5. A L
    Replies:
    1
    Views:
    511
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page