473,382 Members | 1,583 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.

what is wrong with this code?

#include<iostream>

class B; //declaration
class A{
public:
A ( ){ }
A ( B& b){
std::cout<<"A constructor is called\n";
}
};
class B{
public:
operator A() {
std::cout<<"In B, A operator is called\n";
A a;
return a;
}
};
void f (A &a ) { // FIXED if const added

std::cout<<"Ambiguity\n";
}
int main(){

B b;
f(b);

//A a=c(b);

system ("PAUSE");
return 1;
}

Jul 23 '05 #1
8 1957

"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

Now I remember you! You're that troll who simply posts questions for no
apparent purpose. Well, I answered one of your puzzles back in February,
and never got a prize for my work, so I'm not answering any more of them. I
want my cracker, or I'm not doing any tricks! :-)

-Howard
Jul 23 '05 #2

"puzzlecracker" <ir*********@gmail.com> ha scritto nel messaggio
news:11**********************@g14g2000cwa.googlegr oups.com..

f(b) call an object of type A not of type B. B don't derive from A, so this
code do not compile...

Bye
Andrea

Jul 23 '05 #3
thx, what does constant change?

Jul 23 '05 #4
and there is a conversion operator in B class...
Andrea Cacciarru wrote:
"puzzlecracker" <ir*********@gmail.com> ha scritto nel messaggio
news:11**********************@g14g2000cwa.googlegr oups.com..

f(b) call an object of type A not of type B. B don't derive from A, so this code do not compile...

Bye
Andrea


Jul 23 '05 #5

"Andrea Cacciarru" <andrea.cacciarru@~NOSPAM~email.it> ha scritto nel
messaggio news:d4**********@mammooth.localnet...

"puzzlecracker" <ir*********@gmail.com> ha scritto nel messaggio
news:11**********************@g14g2000cwa.googlegr oups.com..

f(b) call an object of type A not of type B. B don't derive from A, so
this code do not compile...

Sorry I have look too much quick your code:

trying:
f((A) b); or f(b.operatorA())

your code compile well. If you don't explicitly cast b, the compiler don't
call conversion operator automatically.

Bye
Andrea
Jul 23 '05 #6
Two problems:

1) Since you are passing by _reference_, that implies that any changes
to the object will affect the object being sent. Making a copy through
a copy constructor or a casting operator will violate that assumption.
That is why adding a const fixes it -- since you are guaranteed to not
be making any changes, everything should work just fine even when
operating on a copy.

2) Since you have both a copy constructor and a casting operator, there
is ambiguity as to which to use.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Jul 23 '05 #7
puzzlecracker wrote:

#include<iostream>

class B; //declaration
class A{
public:
A ( ){ }
A ( B& b){
std::cout<<"A constructor is called\n";
}
};
class B{
public:
operator A() {
std::cout<<"In B, A operator is called\n";
A a;
return a;
}
};
void f (A &a ) { // FIXED if const added

std::cout<<"Ambiguity\n";

}

int main(){

B b;
f(b);

//A a=c(b);

system ("PAUSE");
return 1;

}


What do you think is wrong?

Actually there are 2 problems. My compiler tells me
"A reference that is not to 'const' cannot be bound to a non-lvalue"

After fixing that by adding the 'const' in the function signature of f()
my compiler tells me:

"Ambiguous user-defined-conversion"

But now the questions for YOU?

* Why is the const needed in the first place?
* If the const is added (and thus the first problem solved)
why and what is ambigous?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #8

Jonathan Bartlett wrote:
Two problems:

1) Since you are passing by _reference_, that implies that any changes to the object will affect the object being sent. Making a copy through a copy constructor or a casting operator will violate that assumption. That is why adding a const fixes it -- since you are guaranteed to not be making any changes, everything should work just fine even when
operating on a copy.

2) Since you have both a copy constructor and a casting operator, there is ambiguity as to which to use.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017


thx, the question is whenever I append const to either cntor or
operator compiler has different prefernces without stating ambiguity,
but removing cost from those two causes an error signifying
ambiguity... what is going on?

Jul 23 '05 #9

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

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay ...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.