Question about conversion in expressions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeroen

    #1

    Question about conversion in expressions

    Hi guys,

    Just another question. Suppose I have 2 classes (incomplete code):

    class A {
    A(const B& b);
    A& operator = (const A& a);
    };

    class B {
    B(const A& a);
    friend B operator + (const B& b1, const B& b2);
    };

    If I have code like:

    {
    A x, y, z;

    x = y + z;
    }

    There is no + operator for A. Is it possible that the ctor of B makes
    temporary objects of 'y' and 'z', then the + operator of B is called to
    add x and y, and the result is converted back to class A with its ctor
    and then assigned to x? Or am I talking complete nonsense here?

    Thanx for any pointers,

    Jeroen
  • terminator

    #2
    Re: Question about conversion in expressions

    On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
    Hi guys,
    >
    Just another question. Suppose I have 2 classes (incomplete code):
    >
    class A {
    A(const B& b);
    A& operator = (const A& a);
    >
    };
    >
    class B {
    B(const A& a);
    friend B operator + (const B& b1, const B& b2);
    >
    };
    >
    If I have code like:
    >
    {
    A x, y, z;
    >
    x = y + z;
    >
    }
    >
    There is no + operator for A. Is it possible that the ctor of B makes
    temporary objects of 'y' and 'z', then the + operator of B is called to
    add x and y, and the result is converted back to class A with its ctor
    and then assigned to x? Or am I talking complete nonsense here?
    >
    Thanx for any pointers,
    >
    Jeroen
    The compiler can not deduce how it should add z and y ;there maybe
    other classes similar to B too.I suggest either of the following
    lines:

    x=B(y)+z;
    x=y+B(z);

    in either of these cases in contrast to wht you did the compiler is
    able to pick 'B operator+(const B&,const B&)'.

    Comment

    • Jeroen

      #3
      Re: Question about conversion in expressions

      terminator schreef:
      On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
      >Hi guys,
      >>
      >Just another question. Suppose I have 2 classes (incomplete code):
      >>
      >class A {
      > A(const B& b);
      > A& operator = (const A& a);
      >>
      >};
      >>
      >class B {
      > B(const A& a);
      > friend B operator + (const B& b1, const B& b2);
      >>
      >};
      >>
      >If I have code like:
      >>
      >{
      > A x, y, z;
      >>
      > x = y + z;
      >>
      >}
      >>
      >There is no + operator for A. Is it possible that the ctor of B makes
      >temporary objects of 'y' and 'z', then the + operator of B is called to
      >add x and y, and the result is converted back to class A with its ctor
      >and then assigned to x? Or am I talking complete nonsense here?
      >>
      >Thanx for any pointers,
      >>
      > Jeroen
      >
      The compiler can not deduce how it should add z and y ;there maybe
      other classes similar to B too.I suggest either of the following
      lines:
      >
      x=B(y)+z;
      x=y+B(z);
      >
      in either of these cases in contrast to wht you did the compiler is
      able to pick 'B operator+(const B&,const B&)'.
      >
      OK, so at least one variable should match the available arithmetic
      operator to ensure that it works. Thanks!

      Comment

      • Chris Theis

        #4
        Re: Question about conversion in expressions


        "Jeroen" <no_mail@thanx. comwrote in message news:45e58df7@c s1...
        terminator schreef:
        >On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
        >>Hi guys,
        >>>
        >>Just another question. Suppose I have 2 classes (incomplete code):
        >>>
        >>class A {
        >> A(const B& b);
        >> A& operator = (const A& a);
        >>>
        >>};
        >>>
        >>class B {
        >> B(const A& a);
        >> friend B operator + (const B& b1, const B& b2);
        >>>
        >>};
        >>>
        >>If I have code like:
        >>>
        >>{
        >> A x, y, z;
        >>>
        >> x = y + z;
        >>>
        >>}
        >>>
        >>There is no + operator for A. Is it possible that the ctor of B makes
        >>temporary objects of 'y' and 'z', then the + operator of B is called to
        >>add x and y, and the result is converted back to class A with its ctor
        >>and then assigned to x? Or am I talking complete nonsense here?
        >>>
        >>Thanx for any pointers,
        >>>
        >> Jeroen
        >>
        >The compiler can not deduce how it should add z and y ;there maybe
        >other classes similar to B too.I suggest either of the following
        >lines:
        >>
        >x=B(y)+z;
        >x=y+B(z);
        >>
        >in either of these cases in contrast to wht you did the compiler is
        >able to pick 'B operator+(const B&,const B&)'.
        >>
        >
        OK, so at least one variable should match the available arithmetic
        operator to ensure that it works. Thanks!
        But the question that you should rather ask yourself is should I really do
        this? You're walking towards implicit conversion, which is a dangerous
        thing, and also towards a rather questionable design. If A and B are not
        related, which is what is indicated by your design, then why would you want
        to convert A to B and probably back. It's even more questionable why you
        want the compiler to do this silently. If A & B are not related then they
        also should not share implementations , like the + op for example. I'd
        strongly suggest that you reconsider your design because IMHO this looks
        very much like the beginning of a serious design flaw, which could provide
        you the arguable pleasure of endless debugging hours.

        Cheers
        Chris


        Comment

        • Pete Becker

          #5
          Re: Question about conversion in expressions

          Chris Theis wrote:
          "Jeroen" <no_mail@thanx. comwrote in message news:45e58df7@c s1...
          >terminator schreef:
          >>On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
          >>>Hi guys,
          >>>>
          >>>Just another question. Suppose I have 2 classes (incomplete code):
          >>>>
          >>>class A {
          >>> A(const B& b);
          >>> A& operator = (const A& a);
          >>>>
          >>>};
          >>>>
          >>>class B {
          >>> B(const A& a);
          >>> friend B operator + (const B& b1, const B& b2);
          >>>>
          >>>};
          >>>>
          >>>If I have code like:
          >>>>
          >>>{
          >>> A x, y, z;
          >>>>
          >>> x = y + z;
          >>>>
          >>>}
          >>>>
          >>>There is no + operator for A. Is it possible that the ctor of B makes
          >>>temporary objects of 'y' and 'z', then the + operator of B is called to
          >>>add x and y, and the result is converted back to class A with its ctor
          >>>and then assigned to x? Or am I talking complete nonsense here?
          >>>>
          >>>Thanx for any pointers,
          >>>>
          >>> Jeroen
          >>The compiler can not deduce how it should add z and y ;there maybe
          >>other classes similar to B too.I suggest either of the following
          >>lines:
          >>>
          >>x=B(y)+z;
          >>x=y+B(z);
          >>>
          >>in either of these cases in contrast to wht you did the compiler is
          >>able to pick 'B operator+(const B&,const B&)'.
          >>>
          >OK, so at least one variable should match the available arithmetic
          >operator to ensure that it works. Thanks!
          >
          But the question that you should rather ask yourself is should I really do
          this? You're walking towards implicit conversion, which is a dangerous
          thing,
          Implicit conversion isn't inherently dangerous. It occurs, for example,
          whenever you pass an object of a derived type to a function that takes a
          reference to one of its base types. And implicit conversions among value
          types isn't necessarily dangerous, either.

          The real issue here is the interconvertibi lity of the two types, which
          often leads to mysterious-looking ambiguities.
          and also towards a rather questionable design. If A and B are not
          related, which is what is indicated by your design,
          Seems to me that the sketch we've seen indicates that A and B are
          closely related.

          --

          -- Pete
          Roundhouse Consulting, Ltd. (www.versatilecoding.com)
          Author of "The Standard C++ Library Extensions: a Tutorial and
          Reference." (www.petebecker.com/tr1book)

          Comment

          • Jeroen

            #6
            Re: Question about conversion in expressions

            Pete Becker schreef:
            Chris Theis wrote:
            >"Jeroen" <no_mail@thanx. comwrote in message news:45e58df7@c s1...
            >>terminator schreef:
            >>>On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
            >>>>Hi guys,
            >>>>>
            >>>>Just another question. Suppose I have 2 classes (incomplete code):
            >>>>>
            >>>>class A {
            >>>> A(const B& b);
            >>>> A& operator = (const A& a);
            >>>>>
            >>>>};
            >>>>>
            >>>>class B {
            >>>> B(const A& a);
            >>>> friend B operator + (const B& b1, const B& b2);
            >>>>>
            >>>>};
            >>>>>
            >>>>If I have code like:
            >>>>>
            >>>>{
            >>>> A x, y, z;
            >>>>>
            >>>> x = y + z;
            >>>>>
            >>>>}
            >>>>>
            >>>>There is no + operator for A. Is it possible that the ctor of B makes
            >>>>temporary objects of 'y' and 'z', then the + operator of B is
            >>>>called to
            >>>>add x and y, and the result is converted back to class A with its ctor
            >>>>and then assigned to x? Or am I talking complete nonsense here?
            >>>>>
            >>>>Thanx for any pointers,
            >>>>>
            >>>> Jeroen
            >>>The compiler can not deduce how it should add z and y ;there maybe
            >>>other classes similar to B too.I suggest either of the following
            >>>lines:
            >>>>
            >>>x=B(y)+z;
            >>>x=y+B(z);
            >>>>
            >>>in either of these cases in contrast to wht you did the compiler is
            >>>able to pick 'B operator+(const B&,const B&)'.
            >>>>
            >>OK, so at least one variable should match the available arithmetic
            >>operator to ensure that it works. Thanks!
            >>
            >But the question that you should rather ask yourself is should I
            >really do this? You're walking towards implicit conversion, which is a
            >dangerous thing,
            >
            Implicit conversion isn't inherently dangerous. It occurs, for example,
            whenever you pass an object of a derived type to a function that takes a
            reference to one of its base types. And implicit conversions among value
            types isn't necessarily dangerous, either.
            >
            The real issue here is the interconvertibi lity of the two types, which
            often leads to mysterious-looking ambiguities.
            >
            >and also towards a rather questionable design. If A and B are not
            >related, which is what is indicated by your design,
            >
            Seems to me that the sketch we've seen indicates that A and B are
            closely related.
            >
            Indeed they are closely related. B contains a pointer to an 'A' object,
            and some additional information. I'm playing around with possible
            implementations which may solve several problems of my project. I'm not
            yet up to actually implement things. I'll start with that once I know
            that I have an elegant architecture which will work without problems.
            See my respond in the previous thread 'Question about evaluation order'
            for some info of what I'm doing.

            Thanx,

            Jeroen

            Comment

            • terminator

              #7
              Re: Question about conversion in expressions

              On Feb 28, 5:02 pm, "terminator " <farid.mehr...@ gmail.comwrote:
              On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
              >
              >
              >
              >
              >
              Hi guys,
              >
              Just another question. Suppose I have 2 classes (incomplete code):
              >
              class A {
              A(const B& b);
              A& operator = (const A& a);
              >
              };
              >
              class B {
              B(const A& a);
              friend B operator + (const B& b1, const B& b2);
              >
              };
              >
              If I have code like:
              >
              {
              A x, y, z;
              >
              x = y + z;
              >
              }
              >
              There is no + operator for A. Is it possible that the ctor of B makes
              temporary objects of 'y' and 'z', then the + operator of B is called to
              add x and y, and the result is converted back to class A with its ctor
              and then assigned to x? Or am I talking complete nonsense here?
              >
              Thanx for any pointers,
              >
              Jeroen
              >
              The compiler can not deduce how it should add z and y ;there maybe
              other classes similar to B too.I suggest either of the following
              lines:
              >
              x=B(y)+z;
              x=y+B(z);
              >
              in either of these cases in contrast to wht you did the compiler is
              able to pick 'B operator+(const B&,const B&)'.- Hide quoted text -
              >
              - Show quoted text -
              Your code works unless another class that similar to B defines a
              global binary operator+ and a constructor from an A is introduced.

              Comment

              • terminator

                #8
                Re: Question about conversion in expressions

                On Feb 28, 8:25 pm, "terminator " <farid.mehr...@ gmail.comwrote:
                On Feb 28, 5:02 pm, "terminator " <farid.mehr...@ gmail.comwrote:
                >
                >
                >
                >
                >
                On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
                >
                Hi guys,
                >
                Just another question. Suppose I have 2 classes (incomplete code):
                >
                class A {
                A(const B& b);
                A& operator = (const A& a);
                >
                };
                >
                class B {
                B(const A& a);
                friend B operator + (const B& b1, const B& b2);
                >
                };
                >
                If I have code like:
                >
                {
                A x, y, z;
                >
                x = y + z;
                >
                }
                >
                There is no + operator for A. Is it possible that the ctor of B makes
                temporary objects of 'y' and 'z', then the + operator of B is called to
                add x and y, and the result is converted back to class A with its ctor
                and then assigned to x? Or am I talking complete nonsense here?
                >
                Thanx for any pointers,
                >
                Jeroen
                >
                The compiler can not deduce how it should add z and y ;there maybe
                other classes similar to B too.I suggest either of the following
                lines:
                >
                x=B(y)+z;
                x=y+B(z);
                >
                in either of these cases in contrast to wht you did the compiler is
                able to pick 'B operator+(const B&,const B&)'.- Hide quoted text -
                >
                - Show quoted text -
                >
                Your code works unless another class that similar to B defines a
                global binary operator+ and a constructor from an A is introduced.- Hide quoted text -
                >
                - Show quoted text -
                class B;
                class A{
                public:
                A(){cout<<"A()\ n";};
                A(const B&){cout<<"A(B) \n";};
                };

                class B{
                public:
                B(){cout<<"B()\ n";};
                B(const A&){cout<<"B(A) \n";};
                friend B operator+(const B&,const B&);
                };

                B operator+(const B&,const B&){cout<<"B+B\ n";return B();};

                //if you uncomment the following ,ambiguity error occurs.
                /*class C{
                public:
                C(){cout<<"C()\ n";};
                C(const A&){cout<<"C(A) \n";};
                friend C operator+(const C&,const C&);
                };

                C operator+(const C&,const C&){cout<<"C+C\ n";return C();};*/

                int _tmain(int argc, _TCHAR* argv[])
                {
                A x,y,z;
                x=y+z;//ok:x=A(B(y)+B(z ));
                return 0;
                }

                Comment

                • Chris Theis

                  #9
                  Re: Question about conversion in expressions


                  "Pete Becker" <pete@versatile coding.comwrote in message
                  news:g-idnWYufcAoOnjYn Z2dnUVZ_q-vnZ2d@giganews. com...
                  Implicit conversion isn't inherently dangerous. It occurs, for example,
                  whenever you pass an object of a derived type to a function that takes a
                  reference to one of its base types. And implicit conversions among value
                  types isn't necessarily dangerous, either.
                  I absolutely agree that it's not inherently or necessarily dangerous. But
                  IMO one should be careful as it might open pitfalls that are very often not
                  obvious and hard to spot. Still, it certainly has its place and uses. There
                  is no doubt about that.

                  Cheers
                  Chris


                  Comment

                  • Jeroen

                    #10
                    Re: Question about conversion in expressions

                    terminator schreef:
                    On Feb 28, 8:25 pm, "terminator " <farid.mehr...@ gmail.comwrote:
                    >On Feb 28, 5:02 pm, "terminator " <farid.mehr...@ gmail.comwrote:
                    >>
                    >>
                    >>
                    >>
                    >>
                    >>On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
                    >>>Hi guys,
                    >>>Just another question. Suppose I have 2 classes (incomplete code):
                    >>>class A {
                    >>> A(const B& b);
                    >>> A& operator = (const A& a);
                    >>>};
                    >>>class B {
                    >>> B(const A& a);
                    >>> friend B operator + (const B& b1, const B& b2);
                    >>>};
                    >>>If I have code like:
                    >>>{
                    >>> A x, y, z;
                    >>> x = y + z;
                    >>>}
                    >>>There is no + operator for A. Is it possible that the ctor of B makes
                    >>>temporary objects of 'y' and 'z', then the + operator of B is called to
                    >>>add x and y, and the result is converted back to class A with its ctor
                    >>>and then assigned to x? Or am I talking complete nonsense here?
                    >>>Thanx for any pointers,
                    >>> Jeroen
                    >>The compiler can not deduce how it should add z and y ;there maybe
                    >>other classes similar to B too.I suggest either of the following
                    >>lines:
                    >>x=B(y)+z;
                    >>x=y+B(z);
                    >>in either of these cases in contrast to wht you did the compiler is
                    >>able to pick 'B operator+(const B&,const B&)'.- Hide quoted text -
                    >>- Show quoted text -
                    >Your code works unless another class that similar to B defines a
                    >global binary operator+ and a constructor from an A is introduced.- Hide quoted text -
                    >>
                    >- Show quoted text -
                    >
                    class B;
                    class A{
                    public:
                    A(){cout<<"A()\ n";};
                    A(const B&){cout<<"A(B) \n";};
                    };
                    >
                    class B{
                    public:
                    B(){cout<<"B()\ n";};
                    B(const A&){cout<<"B(A) \n";};
                    friend B operator+(const B&,const B&);
                    };
                    >
                    B operator+(const B&,const B&){cout<<"B+B\ n";return B();};
                    >
                    //if you uncomment the following ,ambiguity error occurs.
                    /*class C{
                    public:
                    C(){cout<<"C()\ n";};
                    C(const A&){cout<<"C(A) \n";};
                    friend C operator+(const C&,const C&);
                    };
                    >
                    C operator+(const C&,const C&){cout<<"C+C\ n";return C();};*/
                    >
                    int _tmain(int argc, _TCHAR* argv[])
                    {
                    A x,y,z;
                    x=y+z;//ok:x=A(B(y)+B(z ));
                    return 0;
                    }
                    >
                    OK, thanks for the example. If I get this right, then C++ will try to do
                    any conversion in order to find a corresponding match with the required
                    operator, but it is a dangerous technique... I will avoid it :-)

                    Jeroen

                    Comment

                    • terminator

                      #11
                      Re: Question about conversion in expressions

                      On Mar 1, 12:03 pm, Jeroen <no_m...@thanx. comwrote:
                      terminator schreef:
                      >
                      >
                      >
                      >
                      >
                      On Feb 28, 8:25 pm, "terminator " <farid.mehr...@ gmail.comwrote:
                      On Feb 28, 5:02 pm, "terminator " <farid.mehr...@ gmail.comwrote:
                      >
                      >On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
                      >>Hi guys,
                      >>Just another question. Suppose I have 2 classes (incomplete code):
                      >>class A {
                      >> A(const B& b);
                      >> A& operator = (const A& a);
                      >>};
                      >>class B {
                      >> B(const A& a);
                      >> friend B operator + (const B& b1, const B& b2);
                      >>};
                      >>If I have code like:
                      >>{
                      >> A x, y, z;
                      >> x = y + z;
                      >>}
                      >>There is no + operator for A. Is it possible that the ctor of B makes
                      >>temporary objects of 'y' and 'z', then the + operator of B is called to
                      >>add x and y, and the result is converted back to class A with its ctor
                      >>and then assigned to x? Or am I talking complete nonsense here?
                      >>Thanx for any pointers,
                      >> Jeroen
                      >The compiler can not deduce how it should add z and y ;there maybe
                      >other classes similar to B too.I suggest either of the following
                      >lines:
                      >x=B(y)+z;
                      >x=y+B(z);
                      >in either of these cases in contrast to wht you did the compiler is
                      >able to pick 'B operator+(const B&,const B&)'.- Hide quoted text -
                      >- Show quoted text -
                      Your code works unless another class that similar to B defines a
                      global binary operator+ and a constructor from an A is introduced.- Hide quoted text -
                      >
                      - Show quoted text -
                      >
                      class B;
                      class A{
                      public:
                      A(){cout<<"A()\ n";};
                      A(const B&){cout<<"A(B) \n";};
                      };
                      >
                      class B{
                      public:
                      B(){cout<<"B()\ n";};
                      B(const A&){cout<<"B(A) \n";};
                      friend B operator+(const B&,const B&);
                      };
                      >
                      B operator+(const B&,const B&){cout<<"B+B\ n";return B();};
                      >
                      //if you uncomment the following ,ambiguity error occurs.
                      /*class C{
                      public:
                      C(){cout<<"C()\ n";};
                      C(const A&){cout<<"C(A) \n";};
                      friend C operator+(const C&,const C&);
                      };
                      >
                      C operator+(const C&,const C&){cout<<"C+C\ n";return C();};*/
                      >
                      int _tmain(int argc, _TCHAR* argv[])
                      {
                      A x,y,z;
                      x=y+z;//ok:x=A(B(y)+B(z ));
                      return 0;
                      }
                      >
                      OK, thanks for the example. If I get this right, then C++ will try to do
                      any conversion in order to find a corresponding match with the required
                      operator, but it is a dangerous technique... I will avoid it :-)
                      >
                      Jeroen- Hide quoted text -
                      >
                      - Show quoted text -
                      if you define 'B' like this:

                      class B{
                      public:
                      B();
                      B(const A&);
                      B operator+(const B&); //binary operator as member function
                      };

                      then you have to write the code this way:

                      {
                      A x,y,z;
                      x=B(x)+z;//OK:x=A(B(x).B:: operator+(B(z)) );
                      x=y+z;//ERROR:no + for A.
                      };


                      Comment

                      Working...