当前位置: 首页 > news >正文

C++_pen_类

类的成员函数

  • 构造函数
  • 析构函数
  • 普通成员函数

构造函数与析构函数

#include <stdio.h>
class STU{
public:STU(){printf("STU\n");}STU(int id){printf("STU(int id)\n");}~STU(){printf("STU Bye!!!\n");}
};int main(int argc, char const *argv[])
{STU a;//调用无参构造函数STU b(5);//显调用STU c = 10;//隐式调用STU d = c;//调用拷贝构造函数STU *p = new STU(1000);//堆中申请return 0;
}

深浅拷贝问题(快捷方式与复制)

浅拷贝

#include <stdio.h>
class STU{
public:STU(){printf("STU\n");p = new char[10];}~STU(){printf("STU Bye!!!\n");delete [] p;}private:char *p;
};int main(int argc, char const *argv[])
{STU a;//调用无参构造函数STU b = a;return 0;
}

在这里插入图片描述

深拷贝

#include <stdio.h>
#include <string.h>
class STU{
public:STU(){printf("STU\n");p = new char[10];strcpy(p, "hello!!!");}STU(const STU &a){//拷贝构造函数printf("const STU &a\n");p = new char[10];strcpy(p, a.p);//拷贝}~STU(){printf("STU Bye!!!\n");delete [] p;}private:char *p;
};int main(int argc, char const *argv[])
{STU a;//调用无参构造函数STU b = a;return 0;
}
http://www.lryc.cn/news/182090.html

相关文章:

  • MySQL 多表关联查询优化实践和原理解析
  • LeNet网络复现
  • Oracle 慢查询排查步骤
  • 互联网Java工程师面试题·MyBatis 篇·第二弹
  • Linux 下如何调试代码
  • 腾讯云服务器简介和使用流程
  • python 二分查找
  • 通过async方式在浏览器中调用web worker
  • FPGA project : TFT_LCD
  • 2023年-华为机试题库B卷(Python)【满分】
  • 创建GCP service账号并管理权限
  • 想要精通算法和SQL的成长之路 - 验证二叉树
  • ERROR 6400 --- [ main] com.zaxxer.hikari.pool.HikariPool : root - Exception
  • CART算法解密:从原理到Python实现
  • C++项目:【高并发内存池】
  • [论文笔记]BitFit
  • 浅谈yolov5中的anchor
  • RabbitMQ-工作队列
  • 网站安全防护措施
  • C++的继承基础和虚继承原理
  • 第三章:最新版零基础学习 PYTHON 教程(第十三节 - Python 运算符—Python 中的运算符函数 - 套装2)
  • Linux网络编程:详解https协议
  • LLVM IR 文档 专门解释 LLVM IR
  • 免费服务器搭建网盘教程,给电脑挂载500G磁盘
  • 【Java】微服务——Nacos配置管理(统一配置管理热更新配置共享Nacos集群搭建)
  • QT基础入门——信号和槽机制(二)
  • 黑豹程序员-架构师学习路线图-百科:JavaScript-网页三剑客
  • 三、互联网技术——IP子网划分
  • TinyWebServer学习笔记-log
  • 【kubernetes】CRI OCI