operator= and implicit type conversion - precedence.

M

Master of C++

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.
 
V

Victor Bazarov

Master of C++ said:
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
 
D

Dave Moore

Master of C++ said:
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
 
M

Master of C++

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.
 
K

Karl Heinz Buchegger

Master said:
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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top