operator= and implicit type conversion - precedence.

Discussion in 'C++' started by Master of C++, Feb 5, 2005.

  1. Hi,

    This is a simple question. In the following example,

    .. class Vector
    .. {
    .. private:
    .. int *Numbers;
    .. int vLength;
    ..
    .. public:
    .. Vector()
    .. Vector(int vLengthP, int *NumbersP = NULL);
    ..
    .. // ...
    ..
    .. Vector& operator=(int scalarTerm);
    .. Vector& operator=(const Vector& another);
    ..
    .. // ...
    ..
    .. };

    If I have statements such as:

    Vector b(4);
    b = 10;

    Can I be 100% sure that the compiler will always call the
    operator=(scalar) for this statement instead of calling the constructor
    Vector(int) (interpreted as an implicit type conversion constructor)
    and then calling operator=(Vector &) ? I tried a simple example with
    g++ and it worked as expected. But I am confused because the type
    conversion operator has precedence over the assignment operator. Or am
    I looking at this in a completely wrong way ? Please explain.

    Thanks in Advance,
    Vijay.

    --
    P.S: My id "Master of C++" has more to do with my favorite album
    "Master of Puppets" than with my proficiency in C++.
     
    Master of C++, Feb 5, 2005
    #1
    1. Advertising

  2. "Master of C++" <> wrote...
    > This is a simple question. In the following example,
    >
    > . class Vector
    > . {
    > . private:
    > . int *Numbers;
    > . int vLength;
    > .
    > . public:
    > . Vector()
    > . Vector(int vLengthP, int *NumbersP = NULL);
    > .
    > . // ...
    > .
    > . Vector& operator=(int scalarTerm);
    > . Vector& operator=(const Vector& another);
    > .
    > . // ...
    > .
    > . };
    >
    > If I have statements such as:
    >
    > Vector b(4);
    > b = 10;
    >
    > Can I be 100% sure that the compiler will always call the
    > operator=(scalar) for this statement instead of calling the constructor
    > Vector(int) (interpreted as an implicit type conversion constructor)
    > and then calling operator=(Vector &) ? I tried a simple example with
    > g++ and it worked as expected. But I am confused because the type
    > conversion operator has precedence over the assignment operator. Or am
    > I looking at this in a completely wrong way ? Please explain.


    The expression in the statement

    b = 10;

    will invoke b.operator=(something). What you need to concern yourself
    with is what that 'something' is an how it is formed.

    There are two functions Vector::eek:perator=. That is called "overloading".
    The compiler has to decide which one to use. It make its determination
    based on the type of the argument provided. '10' has type 'int'. Since
    in case of Vector::eek:perator=(int) the argument is a perfect match, it is
    a better choice than Vector::eek:perator(Vector const&) because that one
    involves a user-defined conversion. Vector::eek:perator=(int) is chosen.

    Operator precedence has no relevance here. It only has relevance when
    operators are both explicit, like in a+b*c [* has higher precedence than
    + so the result is a+(b*c) and not (a+b)*c]. In your case there is no
    conversion operator present until the compiler decides it's needed.

    You need to read about overload resolution.

    Victor
     
    Victor Bazarov, Feb 5, 2005
    #2
    1. Advertising

  3. Master of C++

    Dave Moore Guest

    "Master of C++" <> wrote in message
    news:...
    > Hi,
    >
    > This is a simple question. In the following example,
    >
    > . class Vector
    > . {
    > . private:
    > . int *Numbers;
    > . int vLength;
    > .
    > . public:
    > . Vector()
    > . Vector(int vLengthP, int *NumbersP = NULL);
    > .
    > . // ...
    > .
    > . Vector& operator=(int scalarTerm);
    > . Vector& operator=(const Vector& another);
    > .
    > . // ...
    > .
    > . };
    >
    > If I have statements such as:
    >
    > Vector b(4);
    > b = 10;
    >
    > Can I be 100% sure that the compiler will always call the
    > operator=(scalar) for this statement instead of calling the constructor
    > Vector(int) (interpreted as an implicit type conversion constructor)
    > and then calling operator=(Vector &) ? I tried a simple example with
    > g++ and it worked as expected. But I am confused because the type
    > conversion operator has precedence over the assignment operator. Or am
    > I looking at this in a completely wrong way ? Please explain.
    >


    Well ... if you are worried about this, you should declare all the single
    argument constructors using the 'explicit' keyword. This keyword was added
    to the language to prevent against exactly the kind of implicit conversion
    you are worried about .. or at least that is what I think you are worried
    about 8*).

    HTH,

    Dave Moore
     
    Dave Moore, Feb 5, 2005
    #3
  4. Thanks Dave !! That was exactly what I was looking for. When was this
    added anyway ? I am following the second edition (1991) of Bjarne
    Stroustroup's book and there is no mention of an explicit keyword.

    Thanks,
    Vijay.
     
    Master of C++, Feb 5, 2005
    #4
  5. Master of C++ wrote:
    >
    > Thanks Dave !! That was exactly what I was looking for. When was this
    > added anyway ? I am following the second edition (1991) of Bjarne
    > Stroustroup's book and there is no mention of an explicit keyword.
    >


    Well. 1991 is a long time ago. Don't you think.
    Hint: C++ became a ISO standardizized language in 1998


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Feb 7, 2005
    #5
    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. dmoos AT esigma-systems DOT de

    implicit vs. explicit type conversion for string vs. (char *)

    dmoos AT esigma-systems DOT de, Jun 26, 2003, in forum: C++
    Replies:
    2
    Views:
    2,519
    Default User
    Jun 26, 2003
  2. Replies:
    20
    Views:
    876
    kwikius
    Jul 19, 2006
  3. utab
    Replies:
    3
    Views:
    595
    Juha Nieminen
    Jun 23, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    485
    James Kanze
    Jul 18, 2008
  5. pikalaw
    Replies:
    14
    Views:
    783
    James Kanze
    Jul 26, 2009
Loading...

Share This Page