C++ Class Design
C++ Class Design
目前我正在用以下结构开发我的大多数c++类,我想知道社区是否可以给我一些关于如何改进我的类设计的技巧,使其在未来更可移植,更友好,更易于维护。
#include <vector>
#include <iostream>
namespace CompanyName
{
class Uri;
class Regex;
class String
{
public:
String();
String(const char*);
String(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;
}
}
我想说的是,第一步,也是最好的一步是删除所有你不需要的东西。
考虑Monoliths unstring 的建议:当类被精简到它们的主要概念时,它们更易于维护和"面向未来"。
例如:为什么replace()
需要是一个成员函数?或者to/from_utfX()
函数?
你没有virtual
成员函数,所以你的类显然不打算被多态使用。因此,使析构函数virtual
是不必要的(并导致类具有不必要的v表),并且不需要protected
pimpl。
而且,只要可能,总是选择自由函数(必要时声明为friend
s)作为操作符,而不是成员函数。
相关文章:
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- "Undefined class"作为is_base_of的论据
- 如何将class.cpp和class.hpp编译为一个.o文件
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 如何使用"derived input class"创建派生类?
- 错误:"Left of getValue must have class/struct/union"
- ' class a : b ' 和 ' class a : public b ' 之间的继承类不同
- Pybind11 Class Definition
- 在C++中使用没有合作伙伴Class/Cpp文件的头文件是否实用
- C2011 'CMemDC':Visual Studio 2019中的'class'类型重新定义
- TypeError: [c++ addon class] 不是构造函数
- C++ class template
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- Clearing Class Foo with new(pFoo) Foo()
- C++ class vs a library
- C++ class design
- RAII class design
- C++ Class Design