3

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
  • 1
    Perhaps a code sample? - Anyway, what about void combine(std::string& body, const std::string& prefix, const std::string& suffix);. Inheriting from string adds no value, because you'll have to use the public interface anyway for the implementation. Commented Aug 4, 2011 at 18:49

3 Answers 3

3

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);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you all for quick replies.
For higher performance, you don't need to create a fourth, temporary instance of the string. You can prepend and append ro orig.
You can, but it requires more memory allocations and copies. That will lower performance.
@graphicsMan: not really, the best case w/o extra std::string is faster, the worse case is the same.
Maybe I'm missing some functionality; I'm using insert(orig.begin()...) for the prepend, and insert(orig.end()...) for the append. I just tested this vs my function posted above, and the function included above is many times faster. Interestingly enough, the method of orig = pre + orig + post is only twice as slow as the optimized version, while the prepend and append version is many times slower (gcc 4.6 -O3, 64-bit linux)
2

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.