c++ - String 类的隐式构造函数

标签 c++ class-constructors

字符串 s2("你好");

我必须写一个隐式字符串构造函数吗

字符串::字符串(const char* str);

这个构造函数会处理它吗:

字符串::字符串(const String &str);

最佳答案

当你创建这个字符串对象时,会调用string(const char* s)构造器,所以不需要写构造器。

这是在 std::string 类中定义的构造函数

string();   
string (const string& str); 
string (const string& str, size_t pos, size_t len = npos);  
string (const char* s); 
string (const char* s, size_t n);   
string (size_t n, char c);  
template <class InputIterator>
  string  (InputIterator first, InputIterator last);

此外不要尝试编辑一些标准库,这会导致不必要的问题。如果您想要任何自定义函数,请编写您自己的包装器。

关于c++ - String 类的隐式构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21520861/

相关文章:

c++ - Visual Studio malloc 限制

kotlin - Kotlin 中的构造函数与参数

C++ - 以指针作为数据字段的复制构造函数

c++ - 使用指针隐藏实现(Pimpl 惯用语)

c++ - 如何从重载解析中删除函数?

c++ - 没有类(class)类型

c++ - Qt继承和类构造函数混淆

c++ - 在 C++ Visual Studio 中将字节直接从 float 复制到 unsigned int?

c++ - 在构造函数定义中调用函数后收到 Segmentation Fault 11 错误?

android - Kotlin 抽象类二级构造函数