Function-style cast vs. constructor

后端 未结 3 1630
终归单人心
终归单人心 2021-02-20 14:38

I learned that in C++,

typedef foo* mytype;

(mytype) a        // C-style cast

and

mytype(a)         // function-style cast
         


        
3条回答
  •  -上瘾入骨i
    2021-02-20 15:25

    Syntactically, it is always a cast. That cast may happen to call a constructor:

    char s [] = "Hello";
    // Function-style cast; internally calls std::basic_string::basic_string(char const*, Allocator)
    std::string s2 = std::string(s);
    // C-style cast; internally calls std::basic_string::basic_string(char const*, Allocator)
    std::string s3 = (std::string) s;
    

提交回复
热议问题