内联函数

#pragma once      //头文件 fushu.h
#include <iostream>
class fushu
{
public:
	int x;
	int y;
public:
	fushu();
	~fushu();
	void show();
	inline void  showall(int x, int y);//显式内联
	void   setxy(int x, int y);//若在此处实现函数,则编译器优化,默认隐式内联
	void show(int x, int y);
	/*
	inline void  showall(int x,int y)
	{
		//复合代码
		std::cout << (this->x = x) << (this->y = y) << std::endl;
	}
	*/
};
//内联函数的实现 原则上放在头文件,可去掉inline标识符
//内联函数需要展开,(VS2013是要求放在头文件的)
 void fushu::showall(int x, int y)
{
	std::cout << (this->x = x) << (this->y = y) << std::endl;
}

//::前面的名称 一定是类名或者命名空间
#include "fushu.h"   //fushu.cpp

//::前面必须是类或者命名空间


fushu::fushu()
{
	std::cout << "对象被创建" << std::endl;
}
fushu::~fushu()
{
	std::cout << "对象被销毁" << std::endl;
}
//类调用成员函数,需要明确那个类的对象调用
void fushu::show()
{
	std::cout << "show" << std::endl;
}
void   fushu::setxy(int x, int y)//编译器优化,默认隐式内联
{
	this->x = x;
	this->y = y;
	std::cout << (this->x) << (this->y) << std::endl;
}
void  fushu::show(int x, int y)
{
	std::cout << (this->x) << (this->y) << std::endl;
}

#include <iostream>  //main函数
#include "fushu.h"

void stackrun()
{ 
	fushu fushu1;//对象在栈上  不需回收内存
	fushu1.show();
}

void heaprun()
{
	fushu *pfushu = new fushu;//对象在堆上 必须要回收内存
	pfushu->show();
	pfushu->showall(10, 9);
	pfushu->setxy(19, 29);
	pfushu->show(1, 2);
	//内部成员函数重载,函数指针,明确了参数,
	delete pfushu;
}

void main()
{
	heaprun();

	std::cin.get();
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值