const,static 实例程序
来源:互联网 发布:自然语言处理分类算法 编辑:程序博客网 时间:2024/06/05 20:17
1. 类的静态常变量成员必须在类中申明时候就初始化。并且只有静态常变量能够在申明时候初始化。
// static.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
private:
static const int a=1;
public:
//void add();
void display()
{
cout<<"a="<<a<<endl;
}
};
//int A::a=1;
/*void A::add()
{
a++;
}*/
void main()
{
A a;
//a.add();
a.display();
A b;
//b.add();
b.display();
}
2 类的静态成员要在类外用“类名::静态成员=初始化值”的方式初始化
// static.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
private:
static int a;
public:
void add();
void display()
{
cout<<"a="<<a<<endl;
}
};
int A::a=1;
void A::add()
{
a++;
}
void main()
{
A a;
a.add();
a.display();
A b;
b.add();
b.display();
}
3 const 类成员只能用构造函数初始化列表进行初始化
// static.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
private:
const int a;
public:
A(int x):a(x)
{
}
//void add();
void display()
{
cout<<"a="<<a<<endl;
}
};
/*void A::add()
{
a++;
}*/
void main()
{
A a(3);
//a.add();
a.display();
A b(4);
//b.add();
b.display();
}
- const,static 实例程序
- const, static, const static
- const static 与static const const static
- const、static
- Static & const
- const static
- static const
- const&static
- static /const
- const & static
- static const
- static const
- const static
- static const
- Static Const
- static const、static、const成员
- static、const和static const
- static、const、static const比较
- Javascript & HTML5 使用Ajax自动更新页面数据
- oracle 数据库开发面试题
- struts2之单个文件上传
- Android--相机预览及拍照临时文件/SurfaceView
- [delphi技术] 关于sharemem造成dll错误的处理办法
- const,static 实例程序
- eclipse整合github
- 线程基础知识
- 如何识别图片中的文字
- [Java 10 泛型] 泛型通配符 Info<?> i = new Info<String>(); 在程序中定义没有方法的接口,称之为标识接口
- PAT 1015. 德才论
- Ehcache学习(1)_Ehcache简介
- java XML转JSON格式
- 递归 —— 全排列(带输出具体情况)