Conversion to non-scalar type

  • Follow


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! ]

0
Reply juanjose.garciaripoll (183) 3/3/2010 2:36:30 PM

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! ]

0
Reply Johannes 3/4/2010 4:19:42 AM


1 Replies
793 Views

(page loaded in 0.061 seconds)

Similiar Articles:













7/22/2012 6:38:02 AM


Reply: