Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   conversion from one user defined type to another (http://www.velocityreviews.com/forums/t458047-conversion-from-one-user-defined-type-to-another.html)

 Kavya 10-31-2006 03:30 AM

conversion from one user defined type to another

Here is the code
------------------------
class circle{
private:
public:
circle(int r=0){
}
};
class rectangle{
private:
public:
rectangle(int l,int b){
length-l;
}
operator circle(){
return circle(length);
}
};
int main(){
rectangle r(20,10);
circle c;
c=r;
}

I don't understand what is happening in line c=r. How does this
operator circle( ) function work?

 Victor Bazarov 10-31-2006 03:44 AM

Re: conversion from one user defined type to another

Kavya wrote:
> Here is the code
> ------------------------
> class circle{
> private:
> public:
> circle(int r=0){
> }
> };
> class rectangle{
> private:
> public:
> rectangle(int l,int b){
> length-l;
> }
> operator circle(){
> return circle(length);
> }
> };
> int main(){
> rectangle r(20,10);
> circle c;
> c=r;
> }
>
> I don't understand what is happening in line c=r. How does this
> operator circle( ) function work?

To generate code for the expression 'c=r' the compiler has
several possible choices. Since the left-hand side of the op=
cannot be converted to anything, and it always has to be 'circle',
the compiler has no choice there. There are still several other
choices, however.

First it tries to see how many different 'operator=' there are in
the 'circle' class. It finds only one - the built-in assignment
operator with the signature

circle& circle::operator=(circle const&);

What can it do to adapt the right-hand side so it can be used in
the expression? Possible ways are construct a 'circle' object
from 'rectangle' using a converting c-tor, or (b) convert the
'rectangle' object into a 'circle' or 'circle const&'. The c-tor
version doesn't work, there is no 'circle' c-tor that takes
a rectangle (or a reference to one), so converting the 'rectangle'
is the only choice left.

So, after some searching the compiler arrives at the procedure:
take 'r', call 'operator circle()' on it, take the resulting
temporary, bind a reference to it, pass the reference to the
copy assignment operator.

V
--

 Kavya 10-31-2006 03:50 AM

Re: conversion from one user defined type to another

Victor Bazarov wrote:
> Kavya wrote:
> > Here is the code
> > ------------------------
> > class circle{
> > private:
> > public:
> > circle(int r=0){
> > }
> > };
> > class rectangle{
> > private:
> > public:
> > rectangle(int l,int b){
> > length-l;
> > }
> > operator circle(){
> > return circle(length);
> > }
> > };
> > int main(){
> > rectangle r(20,10);
> > circle c;
> > c=r;
> > }
> >
> > I don't understand what is happening in line c=r. How does this
> > operator circle( ) function work?

>
> To generate code for the expression 'c=r' the compiler has
> several possible choices. Since the left-hand side of the op=
> cannot be converted to anything, and it always has to be 'circle',
> the compiler has no choice there. There are still several other
> choices, however.
>
> First it tries to see how many different 'operator=' there are in
> the 'circle' class. It finds only one - the built-in assignment
> operator with the signature
>
> circle& circle::operator=(circle const&);
>
> What can it do to adapt the right-hand side so it can be used in
> the expression? Possible ways are construct a 'circle' object
> from 'rectangle' using a converting c-tor, or (b) convert the
> 'rectangle' object into a 'circle' or 'circle const&'. The c-tor
> version doesn't work, there is no 'circle' c-tor that takes
> a rectangle (or a reference to one), so converting the 'rectangle'
> is the only choice left.
>
> So, after some searching the compiler arrives at the procedure:
> take 'r', call 'operator circle()' on it, take the resulting
> temporary, bind a reference to it, pass the reference to the
> copy assignment operator.

Thanks you for explaining it so well.

 All times are GMT. The time now is 04:44 PM.