My Clion IDE shows me that there might be a problem while I am transferring a const double*
type to a function which has a parameter const A
when A
defined with a double array as shown below.
When the second constructor of A
is explicit there is a compilation error but when the second constructor of A
is not explicit everything works fine. I assumed that there should be casting from const double*
to const A
but I am not sure. Then I debugged the code and I saw that only the first constructor of A
was called.
So why does it matter if the second constructor is explicit of not?
class A {
public:
friend class B;
private:
A(double v1, double v2): _x{v1, v2}{}
explicit A(const double *arr2): _x{*arr2, *(arr2+1)}{}
double _x[2];
};
class B {
private:
A _b1, _b2;
static double foo(const A &first, const A &second){
return first._x[0]*second._x[1];
}
public:
B(): _b1(2.1, 2.2), _b2(1.2, 1.1){}
B(double List[2][2]): _b1(List[0][0],List[0][1]), _b2(List[1][0], List[1][1])
{}
void Problem( const double* bArr, double* result) const{
double goesTo=thisfunc();
*result =foo(bArr, _b1)/goesTo; //this is where Clion states a possible problem
}
double thisfunc() const{
return foo(_b1, _b2);
}
};
int main() {
double initArray[][2]={{2,1},{1,1}};
B M(initArray);
return 0;
}