Adding function to string class

巧了我就是萌 提交于 2019-12-10 17:56:33

问题


I understand that inheriting from std::string class is a poor idea, but was just trying to add a custom function to string class for a dummy assignment, using inheritance. I want to call my function as 'add' and when I do str.add(str1,str2); it should append str1 at the beginning of the string and str2 at the end of the string. This class(inherited string class) is a private member class of another class(say Parent). when I try to access my string class object with this, it points to the Parent class. How can I do this?

Thanks


回答1:


Be sure you declare your function in public section of class.

Maybe you would love composition over inheritance ;)

    class MyString
    {
           std::string m_string; // do not inherit just composition it
    public:
            explicit MyString(const std::string& str)
                   : m_string(str)
            {
            }

            // your function should be in public scope I think
            MyString& add(const std::string& begin, const std::string& end)
            {
                    m_string.insert(0, begin);
                    m_string.append(end);
                    return *this;
            }

            const std::string& string() const
            {
                    return m_string;
            }
    };

    class Parent
    {
            MyString m_string;
    public:
            void surround(const std::string& begin, const std::string& end)
            {
                    m_string.add(begin, end);
            }
    };

    int main(int argc, char *argv[])
    {
            std::cout << MyString("inherit").add("Do not ", " from std::string!").string() << std::endl;
            return 0;
    }



回答2:


I'm not sure I understand all aspects of your question. When you say a private member class, do you mean private member variable? Or is it privately inheriting? I don't understand "when I try to access my string class object with this, it points to the Parent class".

You're right, inheriting from std::string is probably not a very good idea. First of all, making it a member of a derived string requires that you know quite a lot about the underlying implementation; this may change from distribution to distribution making the code non-portable. If you do write an implementation that is portable, using the already-defined interface provided by std::string, you won't be able to take advantage of any real optimization anyway. Unless you have a really good reason for this, you're better off not doing it at all.

Second, the name "add" is probably not the best, as it doesn't seem to describe what you're doing. "surround" may be a better name.

I think an external function like this might be better, avoiding the whole idea of inheriting from string:

void surround(std::string &orig, std::string const &pre, std::string const &post) {
    orig = pre + orig + post;
}

or, if you want higher performance, do something like this:

void surround(std::string &orig, std::string const &pre, std::string const &post) {
    std::string str;
    str.reserve(orig.size() + pre.size() + post.size());
    str.insert(str.end(), pre.begin(), pre.end());
    str.insert(str.end(), orig.begin(), orig.end());
    str.insert(str.end(), post.begin(), post.end());
    std::swap(str, orig);
}



回答3:


Do not inherit from std::string, that is really a bad idea. You will have to write proper constructors, and never use it with polymorphism, because std::string has no virtual destructor. Just write a free function.



来源:https://stackoverflow.com/questions/6946855/adding-function-to-string-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!