静态变量:

#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class A{ public: A(){ total++; }
static int total; }; //@warn 静态成员变量必须在全局进行定义
int A::total = ; void main()
{
A a1;
A a2; cout << a1.total<< endl;
cout << a2.total<< endl;
  cout << A::total << endl; }

静态成员函数:

#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class A{ public:
A(){ total++; }
static void show()
{
cout << total << endl;
} void echo()
{
cout << total << endl;
} private:
static int total;
int a;
}; int A::total = ; void main()
{
A::show(); A a1;
a1.echo();

A a1,a2;
a1.echo();
a1.show();//也可以通过对象调用静态函数

}

继承后静态成员函数和方法仍旧可用:

#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class A{ public:
A(){ total++; }
static void show()
{
cout << total << endl;
} void echo()
{
cout << total << endl;
} private:
static int total;
int a;
}; class B :public A{ }; int A::total = ; void main()
{
A::show();
B::show(); }

静态成员函数不能说明为虚函数

int(*fun)(int);//声明一个函数指针,指向的函数的参数为int,返回值为int
int*fun(int);//声明一个函数,函数的参数为int,返回值为int型指针

函数指针使用:

#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; int test1(int x,int y){
return x + y;
} int test2(int x, int y){
return x * y;
} void main()
{
int(*p)(int, int);
p = test1;
cout << p(, )<< endl; p = test2;
cout << p(, ) << endl; }

函数指针数组:

#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; int test1(int x,int y){
return x + y;
} int test2(int x, int y){
return x * y;
} void main()
{
int(*p[])(int, int) = { test1, test2 }; cout << p[](,) << endl;
cout << p[](, ) << endl; }

函数指针做参数:

#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; int test1(int x,int y){
return x + y;
} int test2(int x, int y){
return x * y;
} void show(int(*p)(int, int), int x,int y)
{
cout << p(x,y) << endl;
} void main()
{
show(test1, ,);
show(test2, , );
}

typedef来简化定义:

#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; typedef int(*p)(int, int); // p代表 int(*p)(int, int); int test1(int x,int y){
return x + y;
} int test2(int x, int y){
return x * y;
} void show(p vp, int x,int y)
{
cout << vp(x, y) << endl;
} void main()
{
show(test1, ,);
show(test2, , );
}

成员函数指针:

#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class A{
public:
int show(int x, int y){ return x*y; }
}; int(A::*p)(int, int); void main()
{
p = &A::show; A a1;
(a1.*p)(, ); //调用 }

多态和成员函数指针:

#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class human{
public:
virtual void say() = ;
}; class father :public human{
public:
void say(){ cout << "father say:" << endl; }
}; class mother :public human{
public:
void say(){ cout << "mother say:" << endl; }
}; void main()
{
void (human::*fun)()=;
fun = &human::say; human *p;
p = new father;
(p->*fun)(); p = new mother;
(p->*fun)(); }

c++学习-特殊类成员的更多相关文章

  1. C++学习46 getline()函数读入一行字符 一些与输入有关的istream类成员函数

    getline函数的作用是从输入流中读取一行字符,其用法与带3个参数的get函数类似.即    cin.getline(字符数组(或字符指针), 字符个数n, 终止标志字符) [例13.7] 用get ...

  2. C++学习之路—继承与派生(一):基本概念与基类成员的访问属性

    (本文根据<c++程序设计>(谭浩强)总结而成,整理者:华科小涛@http://www.cnblogs.com/hust-ghtao,转载请注明) 1   基本思想与概念 在传统的程序设计 ...

  3. python学习day20 面向对象(二)类成员&成员修饰符

    1.成员 类成员 类变量 绑定方法 类方法 静态方法 属性 实例成员(对象) 实例变量 1.1实例变量 类实例化后的对象内部的变量 1.2类变量 类中的变量,写在类的下一级和方法同一级. 访问方法: ...

  4. 【c# 学习笔记】使用新成员隐藏基类成员

    如果想在派生类中定义与基类成员同名的成员,则可以使用new关键字把基类成员隐藏起来. 如果不适应new关键字,在派生类中定义一个与基类成员同名的成员,编译器将产生警告信息,如下代码演示: public ...

  5. Java学习之多态---类成员变化

    类成员 一.成员变量 编译时:变量(f)所属类(Fu)中是否有成员变量,有:编译成功,没有:编译失败 运行时:变量(f)所属类(Fu)中是否有成员变量,运行该类(Fu)中的成员变量 class Fu ...

  6. python2学习------基础语法3(类、类的继承、类成员函数、防御式编程)

    1.类的定义以及实例化 # 类定义 class p: """ this is a basic class """ basicInfo={&q ...

  7. 再次学习C++类之构造函数

    学习C++类,首先要说C中的结构体,虽然C++类扩展了C中的结构体,可以添加成员函数,但他们是有区别的.在结构体中,成员变量.成员函数都是公有的,而类中,一般是成员变量是私有的,成员函数是公有的,私有 ...

  8. C#定义类成员

    1.成员定义 public--成员可以由任何代码访问. private--成员只能由类中的代码访问(如果没有使用任何关键字,就默认使用这个关键字). internal--成员只能由定义它的程序集(项目 ...

  9. 理解ATL中的一些汇编代码(通过Thunk技术来调用类成员函数)

    我们知道ATL(活动模板库)是一套很小巧高效的COM开发库,它本身的核心文件其实没几个,COM相关的(主要是atlbase.h, atlcom.h),另外还有一个窗口相关的(atlwin.h), 所以 ...

随机推荐

  1. URAL-1989 Subpalindromes(单点更新+hash)

    题目大意:给一行字符串,两种操作:change(pos,char),将pos处字符改为char:isPalindrome(i,j),询问[i,j]之间是否为回文字符串. 题目分析:做正反两次字符串哈希 ...

  2. 深入理解html5系列-文本标签

    转:http://blog.csdn.net/lihui130135/article/details/45150501 文章简介:       关于html5相信大家早已经耳熟能详,但是他真正的意义在 ...

  3. Bootloader的原理以及实现(转载)

    BootLoader工作原理 BootLoader工作原理 BootLoader指系统启动后,在操作系统内核运行之前运行的一段小程序.通过BootLoader,我们可以初始化硬件设备.建立内存空间的映 ...

  4. 29个要点帮你完成java代码优化

    通过java代码规范来优化程序,优化内存使用情况,防止内存泄露 可供程序利用的资源(内存.CPU时间.网络带宽等)是有限的,优化的目的就是让程序用尽可能少的资源完成预定的任务.优化通常包含两方面的内容 ...

  5. lua堆栈操作常用函数学习二

    /* ** basic stack manipulation */ LUA_API int  <strong> (lua_gettop) (lua_State *L);  </str ...

  6. http协议传输二进制数据以及对输入流(php://input)和http请求的理解

    1.index.php <?php $data=file_get_contents('./a.jpg'); $opts = array('http' => array( 'method' ...

  7. [unity3d程序] 纹理扩散

    纹理扩散适合与做游戏的背景(卷轴效果),原理就是让材质贴图动起来(循环运动),代码很简单希望对大家有用 1 2 3 4 5 6 7 8 9 10 11 12 13 // Scroll main tex ...

  8. 003. vs2010发布、打包安装程序(转)

    本资源来自于网络 1.  在vs2010 选择“新建项目”à“ 其他项目类型”à“ Visual Studio Installerà “安装项目”: 命名为:Setup1 . 这是在VS2010中将有 ...

  9. linux xargs参数

    xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具.它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理.通常情况下,xargs从管道或者stdin中读取数据,但是它也能够从 ...

  10. 【转】Solr客户端查询参数总结

    今天还是不会涉及到.Net和数据库操作,主要还是总结Solr 的查询参数,还是那句话,只有先明白了solr的基础内容和查询语法,后续学习solr 的C#和数据库操作,都是水到渠成的事.这里先列出sol ...