您可以訪問類性病的成員函數::字符串,不重複宣告他們在派生類中。
類的簡化版本可以看看下面的方式
#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
你輸入了什麼?新老師?還是適當的設計? –