开发者

C++ Class Design

Currently i'm developing most of my C++ Classes with the following structure and i was wondering if the community could give me some tips on how to improve my class design to make it more portable, friendly, and maintainable in the future.

#include <vector>
#include <iostream>

namespace CompanyName
{
    class Uri;
    class Regex;

    class String
    {
    public:
        String();
        String(const char*);
        S开发者_StackOverflowtring(const String&);

        virtual ~String();

        void trim();
        void erase();
        void remove(const int);
        void remove(const int, const size_t);

        void uppercase();
        void lowercase();

        bool is_null() const;
        bool is_empty() const;

        size_t length() const;

        void append(const String&);            

        bool compare(const String&) const;
        bool compare(const String&, const bool) const;

        void replace(const Regex&, const String&);

        std::vector<String> split(const Regex&) const;

        static const char* to_utf8(const String&);
        static const uint16_t* to_utf16(const String&);
        static const uint32_t* to_utf32(const String&);

        static String from_utf8(const char*);
        static String from_utf16(const uint16_t*);
        static String from_utf32(const uint32_t*);

        static String resource(const Uri&, const int);
        static String resource(const Uri&, const String&);

        String& operator=(String rhs);
        String& operator+(const String& rhs);
        String& operator+=(const String& rhs);

        bool operator==(const String&) const;
        bool operator!=(const String&) const;

        bool operator<(const String&) const;
        bool operator>(const String&) const;

        friend std::ostream& operator<<(std::ostream&, const String&);
        friend std::istream& operator>>(std::istream&, const String&);

        static const String null;
        static const String empty;

    protected:
        struct protected_pimpl;
        protected_pimpl* _protected_pimpl;

    private:
        struct private_pimpl;
        private_pimpl* _private_pimpl;
    };

    //we have to extract the content so as to not expose the UnicodeString.
    inline std::ostream& operator<<(std::ostream& stream, const CompanyName::String& rhs)
    {
        const char* value = String::to_utf8(rhs);
        stream << value;
        delete value;
        return stream;
    }

    inline std::istream& operator>>(std::istream& stream, const CompanyName::String& rhs)
    {
        const char* value = String::to_utf8(rhs);
        stream >> value;
        delete value;
        return stream;
    }
}


I would say that the first, best step would be to remove everything you don't need.

Consider the advice of Monoliths Unstrung: classes are more maintainable and 'future-proof' when they are pruned down to their primary concepts.

For example: why does replace() need to be a member function? Or the to/from_utfX() functions?


You have no virtual member functions, so your class is clearly not meant to be used polymorphically. Thus, making the destructor virtual is unnecessary (and causes the class to have an unnecessary v-table) and there is no need for a protected pimpl.

Also, whenever possible, always prefer free functions (declared as friends if necessary) for operators instead of member functions.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜