【发布时间】:2013-03-30 10:08:27
【问题描述】:
我想用一些功能扩展std::string,所以我从中派生出我的String。为了使String str = stdStr; 之类的代码工作,我尝试重载赋值运算符,但由于某种原因我的代码没有被调用。我该如何解决?
#include <string>
class String
:
public std::string
{
public:
/*
I do know that this constructor will solve the problem, but is it possible to use the operator?
String ( const std::string& stdString )
{
...
}
*/
String& operator= ( const std::string& stdString )
{
...
return *this;
}
};
int main()
{
std::string foo = "foo";
String bar = foo;
return 1;
}
【问题讨论】:
-
你确定继承是正确的扩展方式吗?
-
我认为扩展字符串是个坏主意。不可能说出你的新功能是什么,但我可以非常自信地说继承不是实现它的方法。
-
最糟糕的是你叫它
String -
扩展标准 STL 类通常被认为是一个非常的坏主意。仅在大小写不同的命名是另一个被认为是这样的事物。
-
@UmNyobe 它在我的个人命名空间中。有什么问题?
标签: c++ operator-overloading variable-assignment assignment-operator overloading