By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,929 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,929 IT Pros & Developers. It's quick & easy.

operator= and implicit type conversion - precedence.

P: n/a
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++.

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Master of C++" <ma*************@gmail.com> 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::operator=. 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::operator=(int) the argument is a perfect match, it is
a better choice than Vector::operator(Vector const&) because that one
involves a user-defined conversion. Vector::operator=(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
Jul 23 '05 #2

P: n/a

"Master of C++" <ma*************@gmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
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
Jul 23 '05 #3

P: n/a
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.

Jul 23 '05 #4

P: n/a
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
kb******@gascad.at
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.