This works:
#include<cstdio>
class A{
public:
A(int a):var(a){}
int var;
};
int f(A obj) {
return obj.var;
}
int main() {
std::cout<<f(23); // output: 23
return 0;
}
while this doesn't:
#include<cstdio>
class A{
public:
A(int a, int b):var1(a), var2(b){}
int var1, var2;
};
int f(A obj) {
return (obj.var1 + obj.var2);
}
int main() {
cout<<f(22, 23); // error: conversion from 'int' to
// non-scalar type 'A' requested
return 0;
}
The second piece of code does not work, but I am not able to find a good-enough reason why it doesn't. To me, the code just looks too odd to work.
But what is the actual reason that there is only one implicit conversion allowed in situations like this? Is it a this-is-like-this-only language feature?