473,378 Members | 1,394 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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<toTypeconvertTo(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 ...
May 31 '08 #1
4 1759
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<toTypeconvertTo(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<YARDyards(3);
Length<METREmetres(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(NEWTON,5);

Physical<Combine<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.
May 31 '08 #2
On Jun 1, 5:56*am, girays <selcukgi...@gmail.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<toTypeconvertTo(LengthType toType) const; ???????

template <LengthType toType>
Length<toTypeconvertTo() 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_METERlen1(15);
Length<LENGTH_KMETERlen2 = len1.convertTo<LENGTH_KMETER>();
//std::cout << len2 << std::endl; // need to friend the ostream
inserter
}

--
Best Ragards
Barry
Jun 1 '08 #3
Hello,

girays a écrit :
// Length<toTypeconvertTo(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
Jun 1 '08 #4
On Jun 1, 2:43 am, Gianni Mariani <gi4nos...@mariani.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<toTypeconvertTo(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<YARDyards(3);
Length<METREmetres(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(NEWTON,5);

Physical<Combine<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.
Jun 1 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
10
by: Suki | last post by:
Hi, I'm writing a templated class, and i dont want to use the class otherthan for some predetermined types, say, int, double etc. This class has no meaning for typenames other than those few. ...
1
by: Leslaw Bieniasz | last post by:
Hello, I have the following problem: file a.h --------------- template <class T> class A { // some stuff
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
0
by: Klaus | last post by:
Hi all, I have some questions for template definition: In the code bellow I could write: #1: GenEditor<Mul(&vint, 5); which compiles fine. But for my understanding Mul is not a typename,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.