conversion from one user defined type to another

Discussion in 'C++' started by Kavya, Oct 31, 2006.

  1. Kavya

    Kavya Guest

    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?
    Kavya, Oct 31, 2006
    #1
    1. Advertising

  2. Kavya wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 31, 2006
    #2
    1. Advertising

  3. Kavya

    Kavya Guest

    Victor Bazarov wrote:
    > Kavya wrote:
    > > 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.


    Thanks you for explaining it so well.
    Kavya, Oct 31, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qwerty
    Replies:
    3
    Views:
    9,257
    Scott Allen
    Sep 30, 2004
  2. Oodini
    Replies:
    1
    Views:
    1,735
    Keith Thompson
    Sep 27, 2005
  3. Ram
    Replies:
    8
    Views:
    319
  4. Replies:
    1
    Views:
    438
    Sylvester Hesp
    May 16, 2007
  5. Arne Mertz
    Replies:
    12
    Views:
    711
    Arne Mertz
    Apr 13, 2009
Loading...

Share This Page