Constructor: is this the right call?

In your opinion, which constructor will be called?

class Element {
public:
    Element(bool b = true, bool c = true);
    Element(const std::string s, bool b = true, bool c = true);
};
...
Element element("something", true);

Wrong! The first.

Do I need to restart the Stroustrup book first?

I tried without const, but nothing changes.

Char * seems to be more like bool than std :: string.

With this code, everything is fine:

Element element(string("something"), true);

Compiler: Microsoft Visual C ++ 2010

OS: Windows 7

+3
source share
2 answers

There is a built-in conversion from pointer types to bool, non-zero pointers are converted to, trueand null pointer values ​​to false.

std::string - ( ), const char* std::string const char* bool . - ( , const char *).

Element element( std::string("something"), true);
+7

.

++ (N1905) :

$4.1 , . 4 . :

[...]

- : , , , , , , , boolean.

[...]

$4.4 (13.3.3.1) , , .

4.12

$4.12.1 , , rvalue bool. , null false true.

13.3.3.2

2 ( 13.3.3.1)

- (13.3.3.1.1) ,

- a (13.3.3.1.2) , (13.3.3.1.3).

, .: -)

, :

( ++)

.

+2

Source: https://habr.com/ru/post/1777713/


All Articles