非类型的类模板参数
发布日期:2021-05-10 02:08:15 浏览次数:19 分类:精选文章

本文共 1073 字,大约阅读时间需要 3 分钟。

//main.cpp

#include "pch.h"#include 
#include "stack1.h"#include
using namespace std;int main(){ //类的非类型模板参数 _space3::stack
sint10; for (int i = 0; i < 10; i++) { sint10.push(i); } cout << sint10.top() << endl; // sint10.pop(); cout << sint10.top() << endl; //非类型模板参数的限制 //int o = 20; //_space3::stack
sint10;//不可以,必须是常量值 return 0;}

//stack1.h

#include "pch.h"#include
#include
using namespace std;namespace _space3{ //template
//不能是浮点类型的,类对象也是不可以,或许以后可以 //这是非类型模板参数的一些限制 template
class stack { private: int numls; T elems[MAXSIZE]; public: stack() :numls(0) { cout << "stack() :numls(0)" << endl; } public: bool empty() const { if (numls == 0) { cout << "没有元素!" << endl; } } //压栈 void push(T const& emls) { if (numls == MAXSIZE) { cout << "栈满,无法压栈" << endl; return; } elems[numls] = emls; numls++; } //出栈 void pop() { numls--; } //返回栈顶元素 T top() const { return elems[numls - 1]; } };}
上一篇:linux多线程编程之条件变量
下一篇:新年新目标,越努力越幸运!

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月24日 05时56分12秒