conversion from one user defined type to another

K

Kavya

Here is the code
------------------------
class circle{
private:
int radius;
public:
circle(int r=0){
radius=r;
}
};
class rectangle{
private:
int length,breadth;
public:
rectangle(int l,int b){
length-l;
breadth=b;
}
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?
 
V

Victor Bazarov

Kavya said:
Here is the code
------------------------
class circle{
private:
int radius;
public:
circle(int r=0){
radius=r;
}
};
class rectangle{
private:
int length,breadth;
public:
rectangle(int l,int b){
length-l;
breadth=b;
}
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::eek:perator=(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
 
K

Kavya

Victor said:
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::eek:perator=(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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top