强化练习


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std; class ABCD
{
public:
ABCD(int a, int b, int c)
{
_a = a;
_b = b;
_c = c;
printf("ABCD() construct, a: %d,b: %d,c: %d \n", _a, _b, _c);
}
~ABCD()
{
printf("~ABCD() construct,a: %d,b: %d,c: %d \n", _a, _b, _c);
}
int getA()
{
return _a;
}
private:
int _a;
int _b;
int _c;
}; class MyE
{
public: MyE() :abcd1(1, 2, 3), abcd2(4, 5, 6), m(100)
{
cout << "MyE()" << endl;
}
~MyE()
{
cout << "~MyE()" << endl;
} MyE(const MyE & obj) :abcd1(7, 8, 9), abcd2(10, 11, 12), m(100)
{
printf("MyD(const MyD & obj) \n");
}
public:
ABCD abcd1; //c++编译器不知道如何构造abc1
ABCD abcd2;
const int m;
}; int doThing(MyE mye1)//mye1.拷贝构造(main::myE)
{
printf("doThing() mye1.abc1.a: %d \n", mye1.abcd1.getA());
return 0;
}
int run()
{
MyE myE;
doThing(myE);
return 0;
} int run2()
{
printf("run2 start.. \n");
//ABCD(400, 500, 600); //临时对象的⽣命周期
ABCD abcd = ABCD(100, 200, 300);
printf("run2 end\n");
return 0;
} int main(void)
{
run2();
return 0;
}

强化练习2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std; //构造中调⽤构造是危险的⾏为
class MyTest
{
public:
MyTest(int a, int b, int c)
{
_a = a;
_b = b;
_c = c;
}
MyTest(int a, int b)
{
_a = a;
_b = b;
MyTest(a, b, 100);//创建一个匿名对象
//
}
~MyTest()
{
printf("MyTest~: %d, %d, %d\n", _a, _b, _c);
}
int getC()
{
return _c;
}
void setC(int val)
{
_c = val;
} private:
int _a;
int _b;
int _c;
}; int main()
{
MyTest t1(1, 2);
printf("c: %d\n", t1.getC()); //请问c的值是?
return 0;
}
  • 对象的动态构造和释放

    • malloc free函数,new delete 操作符号
    • 分配基础类型 、分配数组类型、分配对象
    • new和malloc 深入分析,混用测试、异同比较
  • 匿名对象生命周期
  • malloc free函数,new delete 操作符号
  • 分配基础类型 、分配数组类型、分配对象
  • new和malloc 深入分析,混用测试、异同比较
  • 匿名对象总结
    • 匿名对象生命周期
    • 匿名对象去和留
    • 构造中调用构造
  • 匿名对象去和留
  • 构造中调用构造
  • 静态成员变量和静态成员函数(属于类,语法)

new和delete

c与c++的比较

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream> using namespace std; class Test
{
public:
Test()
{
cout << "Test()" << endl;
m_a = 0;
m_b = 0;
}
Test(int a, int b)
{
cout << "Test(int, int)" << endl;
m_a = a;
m_b = b;
}
void printT()
{ cout << "printT:"<<m_a<<","<<m_b << endl;
}
~Test()
{
cout << "~Test()" << endl; }
private:
int m_a;
int m_b;
}; //C语言中
void test1()
{
int *p = (int *)malloc(sizeof(int)); *p = 10;
if (p != NULL) {
free(p);
//delete p;
p = NULL;
} int *array_p = (int *)malloc(sizeof(int)* 10); for (int i = 0; i < 10; i++) {
array_p[i] = i + 1;
} for (int i = 0; i < 10; i++) {
printf("%d ", array_p[i]);
}
printf("\n"); if (array_p != NULL) {
free(array_p);
array_p = NULL;
} cout << "==============" << endl; Test *tp = (Test*)malloc(sizeof(Test));
tp->printT(); if (tp != NULL) {
free(tp);
tp = NULL;
}
} //malloc free 是函数,标准库,stdlib.h
//new 在堆上初始化一个对象的时候,会触发对象的构造函数。malloc不能
//free并不能触发一个对象的析构函数。
//C++中
void test2()
{
int *p = new int;
*p = 10;
if (p != NULL) {
free(p);
p = NULL;
} int *array_p = new int[10];
for (int i = 0; i < 10; i++) {
array_p[i] = i + 1;
} for (int i = 0; i < 10; i++) {
cout << array_p[i]<<" ";
}
cout << endl; if (array_p != NULL) {
delete [] array_p;
} cout << "==========" << endl;
//Test *tp = new Test(10, 20);//触发有参构造
Test *tp = new Test;//触发无惨构造
tp->printT();
if (tp != NULL) {
delete tp;
tp = NULL;
} } int main(void)
{
test1(); cout << "-----------" << endl; test2(); return 0;
}

c++-构造函数练习和delete,new的更多相关文章

  1. 合成的默认构造函数定义为delete的一种情况(针对C++11标准)

    1. 默认初始化 如果定义变量时没有指定初值,则变量会被默认初始化,此时变量被赋予了"默认值". 对于类类型的变量来说,初始化都是依靠构造函数来完成的.因此,即使定义某个类的变量( ...

  2. new 等于 malloc加构造函数

    1.new 是c++中的操作符,malloc是c 中的一个函数 2.new 不止是分配内存,而且会调用类的构造函数,同理delete会调用类的析构函数,而malloc则只分配内存,不会进行初始化类成员 ...

  3. 重载new和delete

    当我们创建一个new表达式时,会发生两件事.首先使用operator new()分配内存,然后调用构造函数.在delete表达式里,调用了析构函数,然后使用operator delete()释放内存. ...

  4. malloc/free和new/delete的异同

    一.基本概念 malloc/free: 1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针.如果分配失败,则返 ...

  5. C++—动态内存管理之深入探究new和delete

    C++中程序存储空间除栈空间和静态区外,每个程序还拥有一个内存池,这部分内存被称为自由空间(free store)或堆(heap).程序用堆来存储动态分配的对象,即,那些程序运行时分配的对象.动态对象 ...

  6. C++动态内存管理之深入探究new和delete

    C++中程序存储空间除栈空间和静态区外,每个程序还拥有一个内存池,这部分内存被称为自由空间(free store)或堆(heap).程序用堆来存储动态分配的对象,即,那些程序运行时分配的对象.动态对象 ...

  7. new malloc和delete free 的区别

    今天看了一个面试题:问new 和 malloc, delete 和 free 的区别,扭捏了半天,也没说完全:现总结如下: 1.先看看new 和 delete 看一个例子: <span styl ...

  8. new/new[]和delete/delete[]是如何分配空间以及释放空间的

    C++中程序存储空间除栈空间和静态区外,每个程序还拥有一个内存池,这部分内存被称为或堆(heap).程序可以用堆来存储动态分配的对象,即那些在程序运行时创建的对象.动态对象的生存期由程序来控制 ,当动 ...

  9. C++内存管理-new,delete,new[],placement new的简单使用

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 首先,我们先看一下C++应用程序,使用memory的途径如下图所示 C++应用程序中申请内存基于分配器的实现(std::allo ...

随机推荐

  1. docker快速部署DNS,实现快速上线

    概念Docker 是一个开源的应用容器引擎,Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.这里我将使用do ...

  2. 【NHOI2018】黑格覆盖

    [题目描述] 在一张由 M * N 个小正方形格子组成的矩形纸张上,有 k 个格子被涂成了黑色.给你一张由 m * n 个同样小正方形组成的矩形卡片,请问该卡片最多能一次性覆盖多少个黑格子? [输入数 ...

  3. 感觉async await 异步编程 并不能提升性能?

    我有2个方法 代码相同 都是执行上传文件IO操作 一个同步 一个异步 接着我用POSTMAN 分别用200个线程 去同时测试2个接口 结果很意外 2个接口 同时执行完成的速度 异步更慢 之前经常看别人 ...

  4. Netty学习——基于netty实现简单的客户端聊天小程序

    Netty学习——基于netty实现简单的客户端聊天小程序 效果图,聊天程序展示 (TCP编程实现) 后端代码: package com.dawa.netty.chatexample; import ...

  5. WPF 使用Win32API 让控件置于WebBrowser上方

    WPF中Webbrowser控件使用HwndHost所以webbrowser会在所有控件的前方.所以webbrowser会覆盖所有同级的控件. 现在通过使用Win32API 可以避免这个情况. 最主要 ...

  6. 漫谈LiteOS之开发板-串口(基于GD32450i-EVAL)

    [摘要] ​主要讲解物联网的技术积累,本期我们先带领大家学习漫谈LiteOS之漫谈开发板第一集-串口,本文基于GD32450i-EVAL对串口以及其通信做了一个简要的分析,以及开发过程中遇到的一些技术 ...

  7. SX1276/SX1278和SXSX1262的详细参数对比

    SX1276/SX1278和SX1262的对比    SX1262是Semtech公司新推出的一款sub-GHz无线收发器.SX1262芯片最大的买点是它的低功耗和超远距离的传输.SX1262接收电流 ...

  8. 强化学习一:Introduction Of Reinforcement Learning

    引言: 最近和实验室的老师做项目要用到强化学习的有关内容,就开始学习强化学习的相关内容了.也不想让自己学习的内容荒废掉,所以想在博客里面记载下来,方便后面复习,也方便和大家交流. 一.强化学习是什么? ...

  9. Remember the Word (UVA-1402)

    Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing ...

  10. ARTS-S pytorch中Conv2d函数padding和stride含义

    padding是输入数据最边缘补0的个数,默认是0,即不补0. stride是进行一次卷积后,特征图滑动几格,默认是1,即滑动一格.