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), 所以 ...
随机推荐
- Json数据,转换规则,
JSON数据转换,规则是遇见json 中的{},则是数组[],遇见name:value,则是'key'=>'value', 但是不带键值的数组如['xxxxxx'],json_encode后仍然 ...
- react 不能往组件中传入属性的值为 undefined
在使用 andt design 的时候遇到个需求,需要清除 Select 组件选中后的值,让它变成什么都没选中,显示 placeholder 刚开始以为设置为 null 即可,结果发现设置为 null ...
- opencv矩阵总结
OpenCV 矩阵操作 CvMat 转自:http://hi.baidu.com/xiaoduo170/blog/item/10fe5e3f0fd252e455e72380.html 每回用矩阵都要查 ...
- 【转】完美解除Windows7的驱动程序强制签名限制
原文网址:http://nick.txtcc.com/index.php/nocategory/290 Windows 7很J,很多驱动程序都无法安装,因为Windows 7不像Vista,必须要求所 ...
- 如何用 matlab 在图片上绘制矩形框 和 添加文字 ?
如何给图像添加矩形框?以及添加想要输入的文字 ? 案例程序,如下所示: clc; close all; clear all;image = imread('/home/wangxiao/Picture ...
- PHP-FPM-failed to ptrace(PEEKDATA) pid 123: Input/output error
If you're running PHP-FPM you can see these kind of errors in your PHP-FPM logs. $ tail -f php-fpm.l ...
- mysql学习之-字符集选定,修改。
基础概念:字符(Character)是指人类语言中最小的表义符号.例如'A'.'B'等:编码(Encoding)是指给定一系列字符,对每个字符赋予一个数值,用数值来代表对应的字符.例如,我们给字符'A ...
- php curl详细说明
CURLOPT_RETURNTRANSFER 选项: curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 如果成功只将结果返回,不自动输出任何内容. 如果失败返回F ...
- 【转】纯CSS设置Checkbox复选框控件的样式
Checkbox复选框是一个可能每一个网站都在使用的HTML元素,但大多数人并不给它们设置样式,所以在绝大多数网站它们看起来是一样的.为什么不把你的网站中的Checkbox设置一个与众不同的样式,甚至 ...
- 【转】第5篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:自动注册JS脚本+委托回调方法分析
作者: 牛A与牛C之间 时间: 2013-11-19 分类: 技术文章 | 暂无评论 | 编辑文章 主页 » 技术文章 » 第5篇:Xilium CefGlue 关于 CLR Object 与 JS ...