473,721 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=(scala r) for this statement instead of calling the constructor
Vector(int) (interpreted as an implicit type conversion constructor)
and then calling operator=(Vecto r &) ? 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 3217
"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=(scala r) for this statement instead of calling the constructor
Vector(int) (interpreted as an implicit type conversion constructor)
and then calling operator=(Vecto r &) ? 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=(som ething). What you need to concern yourself
with is what that 'something' is an how it is formed.

There are two functions Vector::operato r=. That is called "overloadin g".
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::operato r=(int) the argument is a perfect match, it is
a better choice than Vector::operato r(Vector const&) because that one
involves a user-defined conversion. Vector::operato r=(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******** *************@c 13g2000cwb.goog legroups.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=(scala r) for this statement instead of calling the constructor
Vector(int) (interpreted as an implicit type conversion constructor)
and then calling operator=(Vecto r &) ? 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
3380
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? Thanks, Ganesh -- this is an attempt to: -- Remove leading zeroes if @in is all numeric
5
2076
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 operator having arguments having the other class type, but none of the new class type. However it does return a type of the new class. The compiler tells me that at least one parameter must be of the containing type. Is there a way to do this? The other class hos no reference to this new class. Is...
2
18566
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 typecast operator overloading. I am presenting below a summary of what I have gathered. I would appreciate if someone could point out to something that is specific to Borland C++ and is not supported by the ANSI standard. I am also concerned that some of the information may be outdated since...
7
2091
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
3
2032
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 coding was not compliant to the standard in the first place and I did fix many of them, especially with the proper use of the keyword "typename". But I have one problem I have no idea how to fix. I created below a simpler coding which demonstrates my problem: The coding below used to compile...
6
12139
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? nullableDate; nullableDate = (condition) ? null : DateTime.Now; } When the condition is false, I want to return null. If true, I want to return the current date/time.
10
13638
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 supposed to be very strict about types. Doesn't it seem like c# is breaking its own rules? This works: int b = 32; string a = "ABC " + b; Result: a = "ABC 32"
4
2020
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 conversions", the authors have advised the use of named functions that offer conversions instead of conversion operators. In page 87, example 2: Errors that work. class String { // ...
14
1766
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 System.Collections.Generic; using System.Text;
0
9218
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9132
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9067
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8009
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6672
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5986
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3191
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2132
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.