-
Notifications
You must be signed in to change notification settings - Fork 33
/
7.48.txt
32 lines (22 loc) · 1.17 KB
/
7.48.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Exercise 7.48: Assuming the Sales_data constructors are not explicit, what
operations happen during the following definitions
string null_isbn("9-999-99999-9");
Sales_data item1(null_isbn);
Sales_data item2("9-999-99999-9");
What happens if the Sales_data constructors are explicit?
By Faisal Saadatmand
string null_isbn("9-999-99999-9");
A C-style sting literal, i.e. of type const char *, is converted to the library
string object null_isbn.
Sales_data item1(null_isbn);
Explicit conversion of library string null_isbn to Sales_data class-type: the
Sales_data string constructor creates an object item1 with members values:
"9-999-99999-9", 0, 0.
Sales_data item2("9-999-99999-9");
Implicit conversion from a string literal to library string, in which a temp
string is created and passed to Sales_data string constructor. The constructor
then performs an explicit conversion from library string to Sales_data object
item2, same as the previous statement.
If the Sales_data constructors were explicit, the implicit conversion would
still take place since we are using DIRECT initializations as opposed to
copying (with =) and ONLY ONE class-type (implicit) conversion is taking place.