内联函数

xiaoxiao2021-02-28  44

#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(); }
转载请注明原文地址: https://www.6miu.com/read-2621900.html

最新回复(0)