By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,915 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,915 IT Pros & Developers. It's quick & easy.

type conversion w/ template

P: n/a
I current have
template<typename T>
class Vector
{
....
Vector(const Vector& Vin)
{
....
}
...
}
int main(void)
{
Vector<double> V(5);
Vector<int> V1(V);//doesnt work
}

How would I solve this problem?
Thanx.

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
bl**********@gmail.com wrote:
I current have
template<typename T>
class Vector
{
...
Vector(const Vector& Vin)
{
...
}
..
}
int main(void)
{
Vector<double> V(5);
Vector<int> V1(V);//doesnt work
}

How would I solve this problem?


Take a look at constructing from a range of iterators.
The std::vector does it, so could your Vector. Essentially,
you need to define a constructor that would take two arguments,
and construct your vector (sorry, Vector) from them. Make that
constructor a member template:

template<typename T>
class Vector
{
...
template<typename Iterator> Vector(Iterator i1, Iterator i2)
{
storage = ???[std::distance(i1, i2)];
while (i1 != i2)
*storage++ = *it1++;
}
...

I know that it's not necessarily how you implemented your class
(ahem, template), but it should give you the idea.

V
Jul 23 '05 #2

P: n/a
Ian
bl**********@gmail.com wrote:
I current have
template<typename T>
class Vector
{
...
Vector(const Vector& Vin)


This is shorthand for Vector(const Vector<T>& Vin), you can't use a
Vector<double> as it is a completely different class.

You could try a template constructor:

template <typename Other> Vector(const Vector<Other>& Vin).

This assumes Other can be converted to a T somehow, that bit's up to you.

Ian
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.