template class question

G

girays

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 ...
 
G

Gianni Mariani

girays said:
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.
 
B

Barry

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 said:
        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
}
 
V

Vincent Jacques

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,
 
G

girays

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top