类模板、函数模板、其他

类模板、函数模板、其他

static

示例代码:

#ifndef __COMPLEX__
#define __COMPLEX__

class complex
{
   // 成员函数的隐藏参数有this -> 形参不能写.返回值当中可以写可以不写
   public:
  double real() const { return this->re; }
   private:
  double re, im;
};

#endif

实际创建过程当中,由于传入的对象不同,所以返回的real()内容不同

complex c1,c2,c3;
cout << c1.real();
cout << c2.real();

// 编译器实际执行
complex c1,c2,c3;
cout << complex::real(&c1);
cout << complex::real(&c2);

由于传入的指针对象不同.所以返回的real()值不同 -> real()函数只有一份.所以通过this这个指针区分需要处理哪个对象

static

在数据或者函数之前加了static以后会在内存当中单独有一块区域(只有一份) -> 静态数据如利率

static member functionsmember functions的区别在于,static函数没有this这个指针

静态函数如果要处理数据只能去处理静态数据

示例代码:

#pragma once

#ifndef __ACCOUNT__
#define __ACCOUNT__

class Account
{
public:
// 定义静态接口
static double m_rate;
static void set_rate(const double& x) { m_rate = x; }
};
// 定义 -> 分配内存
double Account::m_rate = 8.0;

#endif // !__ACCOUNT__

account-test.cpp:

#include <iostream>
#include "account.h"

int main()
{
Account::set_rate(5.0); // 通过class name调用static函数

Account a;
a.set_rate(7.0); // set是静态函数.所以&a不会变成this作为set_rate函数的形参
}

将构造函数放在private的位置:

class A
{
public:
static A& getInstance() { return a; };
private:
A();
A(const A& rhs);
static A a; // 这个写法当中无论如何都会创建A对象,无论外界是否使用
};

A& A::getInstance()
{
return a;
}

更新改进:

#pragma once

#ifndef __SINGLETON__
#define __SINGLETON__

class A
{
public:
static A& getInstance();
private:
A();
A(const A& rhs);
// static A a; // 如果A不使用则会浪费资源
};

A& A::getInstance()
{
static A a;
return a;
}

#endif // !__SINGLETON__

class template(类模板)

类模板示例:

#pragma once
#ifndef __TEMPCOMPLEX__
#define __TEMPCOMPLEX__

template<typename T> // 告诉编译器T未绑定 -> 这就成了类模板
class temp_complex
{
public:
temp_complex(T r = 0, T i = 0) : re(r), im(i) { };
temp_complex& operator+=(const temp_complex&);
T real() const { return re; }
T imag() const { return im; }
private:
T re, im;

friend temp_complex& __doapl(temp_complex*, temp_complex&);
};

#endif // !__TEMPCOMPLEX__

类模板在头文件声明类之前要写出

function template(函数模板)

示例代码:

#pragma once
#ifndef __STONE__
#define __STONE__

class stone
{
public:
// 一个构造函数和一个操作符重载 -> 无指针、不需要析构函数
stone(int w, int h, int we) : _w(w), _h(h), _weight(we) { }
// <操作符作用左数.传入右数 -> 带有this.this指左数
bool operator< (const stone& rhs) const { return _weight < rhs._weight; }
private:
// 石头的长、宽、重量
int _w, _h, _weight;
};

// 使用函数模板
template <class T>
inline
const T& min(const T& a, const T& b)
{
return b < a ? b : a;
}

#endif // !__STONE__

namespace(命名空间)

语法:

namespace std
{

}

所有的东西被包装在namespace当中 -> 可以理解一块独立区域

如何使用命名空间?

示例代码:(using directive):

#include <iosteam.h>
using namespace std;

int main()
{
cin << ...;
cout << ...;

return 0;
}

如果不使用命名空间:(using declaration)

#include <iosteam.h>
using std::cout;

int main()
{
std::cin << ...;
cout << ...;

return 0;
}
#include <iosteam.h>

int main()
{
std::cin << ...;
std::cout << ...;

return 0;
}

使用命名空间的情况下则不需要使用全名

posted @   俊king  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-03-30 2022/03/29 Mac布置项目环境进行包管理
2021-03-30 计算机系统概论
2021-03-30 3.29Java流程控制语句之循环结构
点击右上角即可分享
微信分享提示