我想扩展 std::string,但不是出于您可能认为的原因

发布于 2024-08-26 02:51:11 字数 107 浏览 4 评论 0原文

我有一个有效地接受字符串的方法。但是,我想要使用的字符串子集非常有限。我正在考虑将 std::string 作为某个类进行 typedef'ing,并显式调用函数。不过,我不确定这是否有效。有想法吗?

I have a method that effectively takes a string. However, there is a very limited subset of strings I want to use. I was thinking of typedef'ing std::string as some class, and call the functions explicit. I'm not sure that would work, however. Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄断弥空 2024-09-02 02:51:11

通常的规则仍然适用:该类不是设计为继承的,并且它的析构函数不是虚拟的,因此如果您向上转换为 std::string 基类,并让该对象被销毁,您的派生类'析构函数不会被调用。

如果你能保证这种事永远不会发生,那就继续吧。

否则,您可以将 std::string 设为类的成员,而不是基类。或者你可以使用私有继承。这种方法的问题在于,您必须重新实现字符串接口才能使该类可用作字符串。

或者您可以定义您的类来公开一个 getString() 函数,该函数返回内部 std::string 对象。然后你仍然可以传递你自己的类,如果你尝试传递 std::string ,编译器会抱怨,但内部字符串在你需要时是可以访问的。这可能是最好的妥协。

The usual rule still applies: the class isn't designed to be inherited from, and its destructor isn't virtual, so if you ever upcast to the std::string base class, and let the object be destroyed, your derived class' destructor won't be called.

If you can guarantee that this will never happen, go ahead.

Otherwise, you could make the std::string a member of your class, rather than a base class. or you could use private inheritance. The problem with this approach is that you'd have to re-implement the string interface for the class to be usable as a string.

Or you could just define your class to expose a getString() function which returns the internal std::string object. Then you can still pass your own class around, and the compiler will complain if you try to pass a std::string, but the internal string is accessible when you need it. That might be the best compromise.

披肩女神 2024-09-02 02:51:11

听起来您想限制输入(即可能是只允许字母的字符串)。

我建议在类中使用字符串,然后包装您想要的函数。

class limited_string
{
    std::string str;

public:
    limited_string(const char *data)
        : str(data)
    {
        if (!valid(str))
            throw std::runtime_exception(); 
    }

    virtual ~limited_string() {}

    limited_string &operator+=(const char *data)
    {
        // do all your work on the side - this way you don't have to rollback
        // if the newly created string is invalid
        std::string newstr(str);
        newstr += data;
        if (!valid(newstr))
            throw std::runtime_exception();

        str = newstr;
    }

    virtual bool valid(const std::string str) = 0;
}

It sounds like you want to limit the inputs (i.e. perhaps a string that only allows letters).

I would recommend using a string within a class and then wrapping the functions you want.

class limited_string
{
    std::string str;

public:
    limited_string(const char *data)
        : str(data)
    {
        if (!valid(str))
            throw std::runtime_exception(); 
    }

    virtual ~limited_string() {}

    limited_string &operator+=(const char *data)
    {
        // do all your work on the side - this way you don't have to rollback
        // if the newly created string is invalid
        std::string newstr(str);
        newstr += data;
        if (!valid(newstr))
            throw std::runtime_exception();

        str = newstr;
    }

    virtual bool valid(const std::string str) = 0;
}
謸气贵蔟 2024-09-02 02:51:11

听起来您有一个接受受限字符串的方法。新班级真的有必要吗?

void needs_restricted_string( std::string const &str ) {
    if ( ! is_restricted_string( str ) ) throw std::runtime_error( "bad str" );
    …
}

否则,您唯一想要捕获的是字符串是否满足限制。我认为 is_restricted_string() 和编译时类之间的唯一区别是性能,如果您有一个专用于检查字符串的存储结构。不要过早优化。也就是说,在新类中放入太多功能或像字符串一样使用它也是错误的。

class restricted_string {
    std::string storage;
public:
     // constructor may implement move or swap() semantics for performance
     // it throws if string is no good
     // not explicit, may be called for implicit conversion
    restricted_string( std::string const & ) throw runtime_error;
     // getter should perhaps be private with needs_restricted as friend
    std::string const &str() const { return storage; }
}; // and no more functionality than that!

void needs_restricted_string( restricted_string const &str ) {
    std::string const &known_restricted = str.str();
}

std::string mystr( "hello" );
needs_restricted_string( mystr );

It sounds like you have one method which accepts a restricted string. Is a new class really necessary?

void needs_restricted_string( std::string const &str ) {
    if ( ! is_restricted_string( str ) ) throw std::runtime_error( "bad str" );
    …
}

Otherwise, the only thing you want to capture is whether a string satisfies the restrictions. The only difference I see between is_restricted_string() and a compile-time class is performance, in the case you have a storage structure dedicated to checked strings. Don't prematurely optimize. That said, it would also be an error to put too much functionality into your new class or to use it like a string.

class restricted_string {
    std::string storage;
public:
     // constructor may implement move or swap() semantics for performance
     // it throws if string is no good
     // not explicit, may be called for implicit conversion
    restricted_string( std::string const & ) throw runtime_error;
     // getter should perhaps be private with needs_restricted as friend
    std::string const &str() const { return storage; }
}; // and no more functionality than that!

void needs_restricted_string( restricted_string const &str ) {
    std::string const &known_restricted = str.str();
}

std::string mystr( "hello" );
needs_restricted_string( mystr );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文