提交 2a8256fb 编写于 作者: xc13262215230's avatar xc13262215230

代替友元函数实现的另一种方式实现<的操作符重载

上级 9c3e4b67
......@@ -150,7 +150,7 @@ namespace doyou {
char* temp = ss.Get("\r\n"); // 实参是常量字符串,存储在内存的常量区
if (temp != nullptr) {
// 请求行示例: "GET /xuchi.php HTTP/1.1\0\n"
_headerMap["RequestLine"] = temp;
_headerMap["RequestLine"] = temp; // const char* 可以被赋值为常量字符串
RequestArgs(temp);
}
......@@ -171,7 +171,7 @@ namespace doyou {
// key: value
// key=Connection
// value=keep-alive
_headerMap[key] = val;
_headerMap[key] = val; // char*类型可以赋值给const char*, 反之编译报错
}
}
else {
......@@ -440,6 +440,8 @@ namespace doyou {
int _bodyLen = 0; // 请求体数据字节总长度
// 分析:采用char*代替std::string可以减少内部new/delete过程,提高程序执行效率,
// 当然了,程序出问题的风险也增加了。
// 分析:当前测试http客户端每秒进行300次数的数据请求,请求行有十个键值对,就是3000次,如果采用
// std::string实现,每秒就会进行3000次的new/delete过程,这是很消耗系统性能的。
std::map<KeyString, char*> _headerMap; // std::string被优化为KeyString
std::map<KeyString, char*> _argsMap; // std::string
RequestType _requestType = HttpClient::UNKNOWN;
......
......@@ -30,16 +30,31 @@ namespace doyou {
return _str;
}
//// 方式一
//bool operator<(KeyString const& right) // this为左值,参数为右值
//{
// return strcmp(this->_str, right._str) < 0; // 在KeyString类的内部可以访问其私有属性
//}
// 方式二
friend bool operator < (KeyString const& left, KeyString const& right); // 当前类的友元函数,在友元函数内部可以访问当前类私有属性或方法
}; // KeyString
// 建议:全局函数推荐写成静态函数更安全
// 方式一
static bool operator < (KeyString const& left, KeyString const& right) // 操作符重载 <
{
return strcmp(left._str, right._str) < 0;
}
//// 方式二: KeyString对象间可以比较大小,并可作为map的key
//static bool operator < (KeyString const& left, KeyString const& right) // 操作符重载 <
//{
// return (left < right);
//}
} // namespace io
} // namespace doyou
......
......@@ -513,8 +513,11 @@ string, 内部都会自动执行new/delete过程,这个过程很消耗系统
93.在visual studio ide工具中,出现编译错误,采用鼠标双击错误鼠标可自动定位
到错误位置。
94.申请内存时,栈很快,堆很慢。
95.c++提供的std标准库函数很好用,但是,在设计服务端的时候,可以采用一些手段
进行性能瓶颈的优化。这个就是c++相对于java/csharp等语言的优势所在。可以兼顾
性能。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册