On 14 Jun, 14:36, Jess <w...@hotmail.comwrote:
Hello,
I can perform implicit conversion through constructor, like
class A{
public:
A(int x):a(x){};
int a;
};
int main(){
A a = 10;
return 0;
}
Your constructor with the signature A(int x) is saying "an object of
type A can be implicitly created from an object or expression of type
int". Implicit conversions from one type to another exist for the
built-in types in the language, and this feature allows you to create
your own types that have the same behaviour.
However, if I have
class A{
public:
A(int x, int y):a(x),b(y){};
int a;
int b;
};
Then the compiler doesn't allow me to convert:
int main(){
A a = (10,20);
This statement tries to use the expression (10,20) to initialise a.
The expression (10,20) has the type int and evaluates to the value 20
(the comma operator evaluates its first argument, 10, and discards it
then evaluates its second argument, 20, and returns that value. The
statement won't compile and the error you get should say something
along the lines of "no constructor available that takes a single int".
Try this code with your original class A definition where the
constructor was A(int x) and it will compile.
return 0;
}
How can I perform implicit conversion through constructor with
multiple arguments?
You can't. Implicit conversion is about converting from one type to
another type. If you have multiple arguments to the constructor there
is no one type to implicitly convert from.
Gavin Deane