构造函数不适用于从std::string继承的类,这个问题是因为C++中的构造函数不能被继承。当一个类从std::string继承时,std::string的构造函数不会被继承到子类中。
为了解决这个问题,可以使用std::string的构造函数初始化列表来初始化子类的基类。例如:
#include<iostream>
#include<string>
class MyString : public std::string {
public:
MyString(const std::string& str) : std::string(str) {}
};
int main() {
MyString my_str("Hello, world!");
std::cout << my_str<< std::endl;
return 0;
}
在这个例子中,MyString类从std::string继承,并使用std::string的构造函数初始化列表来初始化基类。这样就可以正确地初始化MyString类的基类std::string。
领取专属 10元无门槛券
手把手带您无忧上云