您可以访问类性病的成员函数::字符串,不重复宣告他们在派生类中。
类的简化版本可以看看下面的方式
#include <iostream>
#include <string>
int main()
{
class string_extend : public std::string
{
public:
using std::string::string;
using std::string::operator =;
string_extend() : std::string() {}
string_extend(const string_extend &src) : std::string(src) {}
string_extend & operator =(const string_extend &src)
{
std::string::operator =(src);
return *this;
}
};
string_extend s1;
string_extend s2({ 'A', 'B', 'C' });
string_extend s3("Hello");
string_extend s4(8, '*');
std::cout << s1.size() << std::endl;
std::cout << s2.size() << std::endl;
std::cout << s3.size() << std::endl;
std::cout << s4.size() << std::endl;
std::cout << "\"" << s1 << "\"" << std::endl;
std::cout << "\"" << s2 << "\"" << std::endl;
std::cout << "\"" << s3 << "\"" << std::endl;
std::cout << "\"" << s4 << "\"" << std::endl;
s4 += { 'A', 'B', 'C' };
std::cout << "\"" << s4 << "\"" << std::endl;
}
程序输出是
0
3
5
8
""
"ABC"
"Hello"
"********"
"********ABC"
如果你愿意,你也可以添加移动构造函数和移动赋值运算符。
哦亲爱的使用命名空间标准;并从一个标准类继承。时间去找一个新老师...... – Puppy
我没有在这里发布这个部分,但我确实在我的代码中输入了它。 – jombo
你输入了什么?新老师?还是适当的设计? –