静态变量:

#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. JAVA常用系统函数

    1.System.out.println("显示信息"); // 显示内容,并自动换行 2.Syetem.out.print("显示信息"); // 显示内容, ...

  2. 黑马程序员——JAVA基础之多态与Object

    ------- android培训.java培训.期待与您交流! ----------  多态 : 多态定义:  某一类事物的多种存在形态. 多态的体现: 父类的引用指向了自己的子类对象.       ...

  3. 【转】iOS websocket 及时通讯实现

    原文网址:http://blog.csdn.net/manyxh/article/details/48196209 2015-09-03 by 木易哥哥 开发一个类似QQ.微信的即时IM聊天功能,做到 ...

  4. 你足够了解Context吗?

    你足够了解Context吗? 这里有关于Context的一切-写在前面: 当我还是一个24K纯Android新手的时候(现在是也是个小Android萌新),拿着工具书对着电脑敲敲打打,那个时候我就有一 ...

  5. C++重载,重写,重定义

    1.重载:重载是在一个类中,函数名一样,参数类型或参数个数不同的一系列函数需要注意的是,与返回类型无关. class Test { public: void show(); void show(int ...

  6. Myeclipse10中出现Cannot return from outside a function or method错误提示

    最近发现myeclipse10中有几处bug 比如: Cannot return from outside a function or method onClick="return chec ...

  7. nginx 偶发 403原因

    观察errorlog 日志 是否存在类似错误 [error] 12788#0: *322 connection is denied by policyframe[return code:8], 观察是 ...

  8. linux概念之/etc

    [Thu Apr 02 15:04:32 1028 /dev/pts/0 192.168.2.250 /etc]#ll |grep -v "^d"|wc -l99[Thu Apr ...

  9. 虚拟化之intel

    英特尔VT具体包括分别针对处理器.芯片组.网络的VT-X.VT-D和VT-C技术. 处理器:英特尔虚拟化技术(英特尔VT-x),包括英特尔虚拟化灵活迁移技术(Intel VT FlexMigratio ...

  10. 【MySQL】常见的mysql 进程state

    Analyzing 线程是对MyISAM 表的统计信息做分析(例如, ANALYZE TABLE ). checking permissions 线程是检查服务器是否具有所需的权限来执行该语句. Ch ...