what is wrong with this code?

J

Jonathan Bartlett

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
 
P

puzzlecracker

#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;


}
 
H

Howard

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
 
A

Andrea Cacciarru

"puzzlecracker" <[email protected]> ha scritto nel messaggio

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
 
A

Andrea Cacciarru

Andrea Cacciarru said:
"puzzlecracker" <[email protected]> ha scritto nel messaggio

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
 
K

Karl Heinz Buchegger

puzzlecracker said:
#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?
 
P

puzzlecracker

Jonathan said:
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

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top