参考string类完成my_string类
- #include
- #include
- using namespace std;
- class my_string
- {
- private:
- char *str;
- int len;
- public:
- //无参构造
- my_string()
- {
- len = 15;
- str = new char[len];
- cout<<"无参构造"<
- }
- //有参构造
- my_string(char *p)
- {
- len = 15;
- this->str = new char[len];
- memset(str,0,15);
- strcpy(str,p);
- cout<<"有参构造"<
- }
- //拷贝构造
- my_string(const my_string &other)
- {
- len=15;
- this->str=new char[len];
- memset(str,0,15);
- strcpy(str,other.str);
-
- }
- //拷贝赋值
- my_string &operator=(const my_string &other)
- {
- if(&other !=this)
- {
- len=15;
- this->str=new char[len];
- memset(str,0,15);
- strcpy(str,other.str);
- }
-
- }
- //析构函数
- ~my_string()
- {
- delete str;
- cout<<"析构函数"<<" "<<this<
- }
- void show()
- {
- cout<
- }
- //判空
- bool empty()
- {
- if(*str==0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //求总长度
- int size()
- {
- int l=0;
- char *q=str;
- while(*q!=0)
- {l++;
- q++;
- }
-
- return l;
- }
- //at()
- char &at(int pos){}
- //转c风格字符串函数
- char *c_str(){};
- };
- int main()
- {
- char * p ="hello" ;
- my_string s1;
- my_string s2(p);
- s2.show();
- my_string s4(s2);
- s4.show();
- my_string s5;
- s5=s4;
- s5.show();
- cout<
size()< -
-
- return 0;
- }

-
相关阅读:
【SCI征稿】2区数字技术、供应链网络、人工智能、工业物联网智能传感器等领域,进展超顺,仅3个月左右录用
ChinaSkills技能大赛网络系统管理Debian模块(样题一)||RSERVER TASK配置
浅谈快速开发平台:突破系统开发边界,赋能企业数字化!
LeetCode刷题复盘笔记——46. 全排列(一文搞懂回溯解决全排列问题上篇)
国产操作系统之凝思磐石安装
DTLS、ICE--用janus举例
【Netty】七、Netty自定义编码和解码器
学校档案管理系统软件-学校数字档案室解决方案
单片机——通过对P3口地址的操作流水点亮8位LED
完全背包问题
-
原文地址:https://blog.csdn.net/ww1106/article/details/127098687
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU