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

what is wrong with this code?

P: n/a
#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
Share this Question
Share on Google+
8 Replies


P: n/a

"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

P: n/a

"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

P: n/a
thx, what does constant change?

Jul 23 '05 #4

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

P: n/a

"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

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

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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.