c++关键字new delete static 及内存使用,静态成员函数,返回对象本身,连续调用成员函数

之前的创建对象是在栈上创建的,那么如何在堆上创建呢?
那么我们需要关键字"new"

// panduan.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include<math.h>

using namespace std;

void test1()//在堆上开辟空间的三大步骤 开辟 赋值 释放
{
	int *p = (int *)malloc(sizeof(int));
	*p = 10;
	if (p != NULL)
	{
		free(p);
		p = NULL;
	}
	int *array_a = (int *)malloc(sizeof(int) * 10);
	for (int i = 0; i< 10; i++)
	{
		array_a[i] = i + 1;

	}
	for (int i = 0; i < 10; i++)
	{
		printf("%d", array_a[i]);
		printf("\n");
	}
	if (array_a != NULL)
	{
		free(array_a);
		array_a = NULL;
	}
}
void test2()
{
	int *p = new int;//返回int 类型的指针
	if (p != NULL)
	{
		delete p;
		p = NULL;
	}
	int *array_b = new int[10];
	for (int i = 0; i< 10; i++)
	{
		array_b[i] = i + 1;

	}
	for (int i = 0; i < 10; i++)
	{
		cout<<"array_b"<< array_b[i]<<endl;
		printf("\n");
	}
	if (array_b != NULL)
	{
		delete[] array_b;
		array_b = NULL;
	}


}
int main()
{ 
	test2();
	return 0;
}

new malloc free delete 在一般的场景下可以混合使用,但是在处理对象时不可以

static

1.static 成员变量实现了同类对象间信息共享
2.static 成员在类外存储,求类的大小,不包含在内
3.static成员是命名空间属于类的全局变量存储在data区
4.static成员变量只在类外初始化
5.可以通过类名访问(无对象生成时亦可),也可以通过对象访问。

static 修饰的变量只能初始化一次
static修饰的函数只能本文件可见,其他文件不能访问
类内部的static修饰的变量,要在类外边初始化
若static 修饰的变量为私有,则可以在public 中通过函数改变其值

 static void change(int a)
 {
     m_a=a;
 }

Test *tp=new Test(10,20);
在这里插入图片描述
new malloc free delete 在一般的场景下可以混合使用,但是在处理对象时不可以
但是free不能触发析构函数,delete可以触发析构函数

静态成员函数

1.静态成员函数的意义,不在于信息共享,数据沟通,而在于管理静态数据成员,完成对静态数据成员的封装
2.**静态成员函数只能访问静态数据成员。**原因:非静态成员函数在调用时this指针被当作参数传递,而静态成员函数属于类,不属于对象,没有this指针

返回对象本身,连续调用成员方法

// panduan.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include<math.h>

using namespace std;

class Test {
public:
	Test(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	void print()
	{
		cout << "a=" << a << "   b=" << b << endl;
	}
	int getA()
	{
		return a;
	}
	int getB()
	{
		return b;
	}
	Test testadd1(Test &anther)
	{
		Test temp(this->a + anther.getA(), this->b + anther.getB());
		return temp;
	}
	Test& Testadd2(Test &anther)//如果用t1.Testadd2(t2),返回的是t1本身
	{
		this->a += anther.a;
		this->b += anther.b;
		return *this;//如果想返回一个对象本身,用*this返回
	}
	Test Testadd3(Test &anther)//如果用t1.Testadd2(t2),返回的是匿名对象
	{
		this->a += anther.a;
		this->b += anther.b;
		return *this;//如果想返回一个对象本身,用*this返回
	}

private:
	int a;
	int b;
};

Test testadd(Test &t1, Test &t2)
{
	Test temp(t1.getA() + t2.getA(), t2.getA() + t2.getA());
	return temp;
}
	
int main()
{ 
	
		
	Test t1(10, 20);
	Test t2(30, 40);
	Test t3=testadd(t1, t2);
	t3.print();
	cout << "''''''''''''''''''''''''''''" << endl;
	Test t4 = t1.testadd1(t2);
	t4.print();
	cout << "''''''''''''''''''''''''''''" << endl;
	t1.Testadd2(t2);
	t1.print();
	cout << "''''''''''''''''''''''''''''" << endl;
	//如果想对一个对象连续调用成员方法,每次对象都会改变对象本身,成员方法需要返回引用
	t1.Testadd2(t2).Testadd2(t2);
	t1.print();
	cout << "''''''''''''''''''''''''''''" << endl;
	t1.Testadd3(t2).Testadd3(t2);//只在t1上加了一次
	t1.print();
	return 0;

}

发布了35 篇原创文章 · 获赞 2 · 访问量 2424

猜你喜欢

转载自blog.csdn.net/weixin_41375103/article/details/104335004
今日推荐