Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能重复: C++ 中的显式关键字是什么意思?
关键字显式是什么意思?
只有一个参数的 C++ 构造函数会自动执行隐式类型转换。例如,如果在构造函数需要字符串指针参数时传递一个 int,编译器将添加将 int 转换为字符串指针所需的代码。但是,您可能并不总是想要这种自动行为。
您可以在构造函数声明中添加显式以防止隐式转换。这会强制代码要么使用正确类型的参数,要么将参数转换为正确的类型。也就是说,如果演员表在代码中没有明显表达,则会导致错误。
显式 (C++)