Typically, the member functions of the Standard Library class string which take a string argument, are overloaded with both the parameter types const String& as well as const char*. Why is this so?
Also Given a class Complex, for complex numbers, which has a conversion from double, can we say that using two overloadings of the respective functions is worthwhile?
Given a class Complex, for complex numbers, which has a conversion from double, can we say that using two overloadings of the respective functions is worthwhile?
to prevent implicit conversions from double (d) to Complex (c = d + 0*i), even a Complex ctor with one argument should be qualified explicit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
struct Complex
{
double m_real;
double m_imaginary;
explicit Complex(double real): m_real (real), m_imaginary(0){};
};
int main()
{
Complex c = 7;//error: conversion from 'int' to non-scalar type 'Complex' requested
//but w/o explicit keyword this program would compile fine, copy intialization selects Complex::Complex(double)
}
thank you for your reply but I am still not sure what you meant....I am sorry for my lack of understanding, can you please elaborate and explain in a bit more detail both part a and b?