c++学习-特殊类成员
静态变量:
#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++学习-特殊类成员的更多相关文章
- C++学习46 getline()函数读入一行字符 一些与输入有关的istream类成员函数
getline函数的作用是从输入流中读取一行字符,其用法与带3个参数的get函数类似.即 cin.getline(字符数组(或字符指针), 字符个数n, 终止标志字符) [例13.7] 用get ...
- C++学习之路—继承与派生(一):基本概念与基类成员的访问属性
(本文根据<c++程序设计>(谭浩强)总结而成,整理者:华科小涛@http://www.cnblogs.com/hust-ghtao,转载请注明) 1 基本思想与概念 在传统的程序设计 ...
- python学习day20 面向对象(二)类成员&成员修饰符
1.成员 类成员 类变量 绑定方法 类方法 静态方法 属性 实例成员(对象) 实例变量 1.1实例变量 类实例化后的对象内部的变量 1.2类变量 类中的变量,写在类的下一级和方法同一级. 访问方法: ...
- 【c# 学习笔记】使用新成员隐藏基类成员
如果想在派生类中定义与基类成员同名的成员,则可以使用new关键字把基类成员隐藏起来. 如果不适应new关键字,在派生类中定义一个与基类成员同名的成员,编译器将产生警告信息,如下代码演示: public ...
- Java学习之多态---类成员变化
类成员 一.成员变量 编译时:变量(f)所属类(Fu)中是否有成员变量,有:编译成功,没有:编译失败 运行时:变量(f)所属类(Fu)中是否有成员变量,运行该类(Fu)中的成员变量 class Fu ...
- python2学习------基础语法3(类、类的继承、类成员函数、防御式编程)
1.类的定义以及实例化 # 类定义 class p: """ this is a basic class """ basicInfo={&q ...
- 再次学习C++类之构造函数
学习C++类,首先要说C中的结构体,虽然C++类扩展了C中的结构体,可以添加成员函数,但他们是有区别的.在结构体中,成员变量.成员函数都是公有的,而类中,一般是成员变量是私有的,成员函数是公有的,私有 ...
- C#定义类成员
1.成员定义 public--成员可以由任何代码访问. private--成员只能由类中的代码访问(如果没有使用任何关键字,就默认使用这个关键字). internal--成员只能由定义它的程序集(项目 ...
- 理解ATL中的一些汇编代码(通过Thunk技术来调用类成员函数)
我们知道ATL(活动模板库)是一套很小巧高效的COM开发库,它本身的核心文件其实没几个,COM相关的(主要是atlbase.h, atlcom.h),另外还有一个窗口相关的(atlwin.h), 所以 ...
随机推荐
- JS中的自定义属性
<div id="div1" a="a" data-bbb="bbb">div</div> <script&g ...
- kuangbin_ShortPath J (POJ 1511)
其实虽然一开始有被这个题的8000MS 和 256MB限制又被吓到 但是严格来说跟之前的POJ 3268是一样的做法只是数据大了点 但是问题就出在数据大了点上 其实严格来说也不大 1e6 数组加起来大 ...
- 【转】非常适用的Sourceinsight插件,提高效率事半功倍
原文网址:http://www.cnblogs.com/wangqiguo/p/3713211.html 一直使用sourceinsight编辑C/C++代码,sourceinsight是一个非常好用 ...
- x86-64_register_and_function_frame.html
http://www.searchtb.com/2013/03/x86-64_register_and_function_frame.html
- Unity代码热更新方案 JSBinding + SharpKit 首页
目前Unity的代码更新方案有很多,主要以lua为主. JSBinding + SharpKit 是一种新的技术,他做了两件事情: JSBinding将C#导出到 JavaScript (引擎是 Mo ...
- struts2标签整理
在JSP2.1中#被用作了JSP EL(表达式语言)的特殊记好,所以对OGNL的使用可能导致问题, 一个简单的方法是禁用JSP2.1的EL特性,这需要修改web.xml文件: Java代码 ...
- echarts简单使用
最近在做一个项目,开始使用的是acharts,在电脑端访问的效果还真不错,但是放到手机端访问就惨了,尤其是iOS系统上,各种不兼容,后来准备换收费的hightcharsts,无意间发现有个免费的ech ...
- PHP使用mail()函数发送邮件流程以及注意事项
1. 配置 php.ini sendmail_path = /usr/sbin/sendmail -f yourname@sth.com -t -i 2. 使用函数 mail($to, $subjec ...
- 给 input 中 type="text" 设置CSS样式
input[type="text"], input[type="password"] { border: 1px solid #ccc; paddi ...
- linux服务之drbd
http://www.drbd.org/docs/about/http://oss.linbit.com/drbd/ 一般我们会在生产环境的MYSQL中用drbd +ha做master 备份,当然这是 ...