I've been writing c for several years, just get into c++ for some needs! My book is C++ Primer 5/e, and I've reach the topic about constructor conversion and got some problems
In C++ we have many kinds of casting. like static_cast, dynamic_cast, and we also have function-like casting which is a kind of explicit casting.
class Sales_data{
public:
//C++11 feature,ask compiler to create a default constructor for us
Sales_data() = default;
Sales_data(const std::string &s, unsigned n, double p):
bookNo(s), units_sold(n), revenue(p*n);
explicit Sales_data(const std::string &s): bookNo(s){}
explicit Sales_data(std::istream&);
Sales_data combine(const Sales_data &item);
private:
std::string bookNo; // implicitly initialized to the empty string
unsigned units_sold;
double revenue;
};
std::string s is an ISBN. and the forth one read a ISBN from stdin
I know without explicit, the following statements is legal.
item.combine(string("ISBN NUMBER"));
item.combine(cin);
//error: need two conversion const char *->temporary string-> temporary Sales_item
item.combine("ISBN NUMBER");
I just don't get what are the benefits of making the last two of the constructors explicit?
http://www.cplusplus.com/reference/vector/vector/vector/
I can't get the point why some of vector's constructors are declared explicit?? What is the reason?