472,353 Members | 1,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

operator= and implicit type conversion - precedence.

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
4 3085
"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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ganesan Rajaraman | last post by:
In the following examples, why the first case statement works and why the second doesn't, I wonder. Could anyone shed some light on this? ...
5
by: bsaucer | last post by:
I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an...
2
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents a rather good discussion of...
7
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
3
by: CoolPint | last post by:
After upgrading to gcc 3.4.2 from gcc 3.2.3, I got compiler errors that I could not figure out. After reading other postings, I learned that my...
6
by: Chris Dunaway | last post by:
Consider this code (.Net 2.0) which uses a nullable type: private void button1_Click(object sender, System.EventArgs e) { DateTime?...
10
by: =?Utf-8?B?RWxlbmE=?= | last post by:
I am surprised to discover that c# automatically converts an integer to a string when concatenating with the "+" operator. I thought c# was...
4
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit...
14
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.