From: Juanjo on
Suppose I have a template class Vector<t> and I define a child

class Indices : public Vector<int> {
public:
Indices(const Vector<int> &v);
....
}

Now class StaticVector<int,n> has an operator Vector<int>() method.

Why do I get then messages such as "conversion from StaticVector<int,
2> to non-scalar type Indices requested"???

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Johannes Schaub (litb) on
Juanjo wrote:

> Suppose I have a template class Vector<t> and I define a child
>
> class Indices : public Vector<int> {
> public:
> Indices(const Vector<int> &v);
> ...
> }
>
> Now class StaticVector<int,n> has an operator Vector<int>() method.
>
> Why do I get then messages such as "conversion from StaticVector<int,
> 2> to non-scalar type Indices requested"???
>

Because you are probably doing copy initialization ("=" form). Try

Indices i(staticvector);

If you use "= staticvector;" or if you try to pass it to a function
expecting Indices it might fail, because for it to work not only it needs to
convert from staticvector to Indices using Indices' constructor, but during
this initialization, a second conversion is needed from staticvecor to
Vector<int>. Two user defined conversions in one such single sequence is not
allowed.

Perhaps write a function "fromStaticVector" that converts a static to a
dynamic vector, and pass the result to Indices.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]