开发者

Adding function to string class

开发者 https://www.devze.com 2023-03-26 04:01 出处:网络
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 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


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);
}


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.


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;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消