473,382 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

automatic type conversion

Hi Everyone,

I have the following code,
class B;

class A
{
public : operator B();
};

class B
{
public : B()
{
}
B(const A& ref)
{
printf("destination conversion\n");
}

B(const B& ref)
{
printf("copy constructor\n");
}
};

A::operator B()
{
printf("source conversion\n");
return B();
}
int main()
{
A obj;
B obj1 = obj;
return(0);
}

so the output is

destination conversion.

There are actually two ways to convert from obj to obj1
1) destination conversion, constructor function of B
2) source conversion, operator function of A and then copy constructor
of B

The compiler has selected the first option, where as i expected a
ambiguity compile time error, what does the standard say for this?
Dec 11 '07 #1
6 2610
Rahul wrote:
Hi Everyone,

I have the following code,
class B;

class A
{
public : operator B();
};

class B
{
public : B()
{
}
B(const A& ref)
{
printf("destination conversion\n");
}

B(const B& ref)
{
printf("copy constructor\n");
}
};

A::operator B()
{
printf("source conversion\n");
return B();
}
int main()
{
A obj;
B obj1 = obj;
return(0);
}

so the output is

destination conversion.

There are actually two ways to convert from obj to obj1
1) destination conversion, constructor function of B
2) source conversion, operator function of A and then copy constructor
of B

The compiler has selected the first option, where as i expected a
ambiguity compile time error, what does the standard say for this?
Your declaration/definition/initialisation statement

B obj1 = obj;

is actually another form of writing

B obj1((B(obj)));

(and the copying from the temporary of type B to 'obj1' is allowed
to be optimized away by the compiler). As you can see, the temporary
is created from 'obj'. There are two ways to create it: by means of
the converting constructor in B or by means of creating *another*
temporary from A::operator B() and then copy-constructing the original
temporary. The compiler chooses the short way, I guess.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 11 '07 #2
On Dec 11, 11:33 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Rahul wrote:
Hi Everyone,
I have the following code,
class B;
class A
{
public : operator B();
};
class B
{
public : B()
{
}
B(const A& ref)
{
printf("destination conversion\n");
}
B(const B& ref)
{
printf("copy constructor\n");
}
};
A::operator B()
{
printf("source conversion\n");
return B();
}
int main()
{
A obj;
B obj1 = obj;
return(0);
}
so the output is
destination conversion.
There are actually two ways to convert from obj to obj1
1) destination conversion, constructor function of B
2) source conversion, operator function of A and then copy constructor
of B
The compiler has selected the first option, where as i expected a
ambiguity compile time error, what does the standard say for this?

Your declaration/definition/initialisation statement

B obj1 = obj;

is actually another form of writing

B obj1((B(obj)));

(and the copying from the temporary of type B to 'obj1' is allowed
to be optimized away by the compiler). As you can see, the temporary
is created from 'obj'. There are two ways to create it: by means of
the converting constructor in B or by means of creating *another*
temporary from A::operator B() and then copy-constructing the original
temporary. The compiler chooses the short way, I guess.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
But the moment i have the following code,

A obj;
B obj1;
obj1 = obj;

I get an error, and the compiler is not able to decide between the
constructor conversion and the operator conversion... So i expected a
similar error in the initial posted code... Does anyone know what the
standard says for these cases?
Dec 11 '07 #3
Rahul wrote:
>
class B;

class A
{
public : operator B();
};

class B
{
public : B()
{
}
B(const A& ref)
{
printf("destination conversion\n");
}
[remainder redactged]
This is homework. va*******@gmail.com posted the exact same question.

To vairavans and Rahul, please see FAQ 5.2:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.2
Dec 11 '07 #4
On Dec 12, 12:14 am, red floyd <no.s...@here.dudewrote:
Rahul wrote:
class B;
class A
{
public : operator B();
};
class B
{
public : B()
{
}
B(const A& ref)
{
printf("destination conversion\n");
}
[remainder redactged]

This is homework. vairav...@gmail.com posted the exact same question.

To vairavans and Rahul, please see FAQ 5.2:http://www.parashift.com/c++-faq-lit...t.html#faq-5.2
I don't understand what possible home work do u make out of it?
I'm just trying to understand cases where compiler spots an
ambiguity... besides the complexity just increases with operator=,
copy constructor and their combination with source converter or
destination converter or both...
Naturally one would like to clear out the basic simple cases
first...
Dec 11 '07 #5
Rahul wrote:
On Dec 12, 12:14 am, red floyd <no.s...@here.dudewrote:
>Rahul wrote:
>>class B;
class A
{
public : operator B();
};
class B
{
public : B()
{
}
B(const A& ref)
{
printf("destination conversion\n");
}
[remainder redactged]
This is homework. vairav...@gmail.com posted the exact same question.

To vairavans and Rahul, please see FAQ 5.2:http://www.parashift.com/c++-faq-lit...t.html#faq-5.2

I don't understand what possible home work do u make out of it?
I'm just trying to understand cases where compiler spots an
ambiguity... besides the complexity just increases with operator=,
copy constructor and their combination with source converter or
destination converter or both...
Naturally one would like to clear out the basic simple cases
first...

If it's not homework, then why did you and vairavans post identical code
(down to formatting!) with the same question?
Dec 11 '07 #6
On Dec 11, 7:28 pm, Rahul <sam_...@yahoo.co.inwrote:
I have the following code,
class B;
class A
{
public : operator B();
};
class B
{
public : B()
{
}
B(const A& ref)
{
printf("destination conversion\n");
}
B(const B& ref)
{
printf("copy constructor\n");
}
};
A::operator B()
{
printf("source conversion\n");
return B();}
int main()
{
A obj;
B obj1 = obj;
return(0);
}
so the output is
destination conversion.
There are actually two ways to convert from obj to obj1
1) destination conversion, constructor function of B
2) source conversion, operator function of A and then copy constructor
of B
The compiler has selected the first option, where as I
expected a ambiguity compile time error, what does the
standard say for this?
That you should see "source conversion" (which is also what I
get from g++ and Sun CC). The requirement in "B obj1 = obj;" is
that obj must be converted into a B (which is then used as an
argument for the copy constructor). A::operator B() is an exact
match, in every detail. B( A const& ) is classified as an exact
match, but does require adding a const, which in this case, the
compiler shoul use as a tie-breaker.

Change the code to:
A const obj ;
B obj1 = obj ;
and you should see "destination conversion", since A::operator
B() can no longer be called on obj. (You'll also have to
provide a user defined default constructor for A.)

Declare A::operator B() const (which it probablyl should be),
and the conversions will be ambiguous (but g++ allows it, using
"destination conversions"---an error in g++).

In practice, of course, it shouldn't matter---you should never
provide round-trip implicit conversions anyway.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 12 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
7
by: bartek | last post by:
Please consider the following scenario below (sketch of). There are two templates defined: A and B, both with mutual conversion operators defined. Also, there's a free function template...
4
by: Jason Heyes | last post by:
I want Foo-type objects to be convertible to Bar-type objects. Can I use operator Bar() to do this just as I would use operator bool() to convert to boolean? Example: class Foo { public:...
1
by: Aaron | last post by:
I know now (thanks to this board) that there are numerous ways to force a type conversion from a string to a number (such as +, Number(), adding 1, parseInt(), etc). I'm curious, though...is there...
1
by: Philip Bondi | last post by:
Hello to all SQL Server junkies who work with non-English characters: For people running scripts from the command line using ANSI files with special characters, it is very important to use isql...
7
by: Ben R. | last post by:
How does automatic type casting happen in vb.net? I notice that databinder.eval "uses reflectoin" to find out the type it's dealing with. Does vb.net do the same thing behind the scenes when an...
2
by: Partha Mandayam | last post by:
Is there a tool for asp.net 1.1 to asp.net 2.0 automatic conversion?
3
by: Russ | last post by:
I posted a message on this several days ago, but it apparently got lost in googlespace, so I'll try it again. I recently discovered a bug in my code that apparently resulted from the automatic...
3
by: Markus Dehmann | last post by:
I think this is a question about automatic type conversion, but I didn't find the answer after googling for these words ... I have a class called Value (source see below) which can hold an int...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.