20140903 dynamic_cast和static的区别 链表的冒泡排序和插入排序
1、四个类型转换
http://www.cnblogs.com/chio/archive/2007/07/18/822389.html
static_cast和dynamic_cast的区别
- dynamic_cast的向下转化必须要有虚函数,
- 对于向上转化(子类->父类),两者是相同的。dynamic_cast<Base *>(&Derived)和static_cast<Base *>(&Derived)
- 对于向下转化(父类指针->子类指针),如果父类指针指向的是子类对象,那么两者相同;如果父类指针指向的是父类对象,那么static转换会不安全,dynamic_cast会返回NULL;
#include<iostream>
using namespace std;
class Base
{
public:
int m_num;
void fBase()
{
cout<<"Base is called"<<endl;
}
/*virtual void fBase1()
{ }*/
}; class Derived :public Base
{
public:
char *m_szName[100];
void fDeviced()
{
cout<<"Derived is called"<<endl;
}
}; int main()
{
Base B;
Derived D;
Base *pb=&B;
Base *pb1=&D;
/*对于上行转化,dynamic_cast和static_cast是一样的,因为指针pb只会访问基类的内容*/
pb=dynamic_cast<Base *>(&D);
pb=static_cast<Base *>(&D);
//*以下两句话的效果是一样的,因为pb1本身指向的是派生类对象,所以转化后,pd1和pd2的访问不会越界*/
pd1=static_cast<Derived *>(pb1);
pd2=dynamic_cast<Derived *>(pb1);
//以下两句话的效果是不一样的,因为pb本身指向的是基类对象,所以转化后,pd1成功,但是通过pd1可以访问派生类的成员m_szName,这是不安全的
//而dynamic_cast的会使pd2变成0,即不可以访问类的成员
Derived *pd1=static_cast<Derived *>(pb);
Derived *pd2=dynamic_cast<Derived *>(pb);
}

2、链表冒泡排序算法
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(listnode)
struct listnode
{
int data;
struct listnode *next;
}; listnode *Create()
{
listnode *head=(listnode *)malloc(LEN);
head->data=0;
head->next=NULL;
listnode *rear=head;
int x=0;
int flag=1;
while(flag)
{
scanf("%d",&x);
if(x!=0)
{
listnode *s=(listnode *)malloc(LEN);
s->data=x;
rear->next=s;
rear=s;
}
else
flag=0;
}
rear->next=NULL;
return head;
}
void bubblesort(listnode * head)
{
listnode * end, * p , * q;//end用来记录排好序的最后一个元素地址,p,q分别为前驱,后继
int temp;
p=head->next;
q=p->next;
end=NULL;
while(end!=head->next)
//如果head所指结点的next成员为end,循环结束
{
p=head->next;//p结点从链表头结点开始
q=p->next;//q指向p所指结点的下一个结点
while(p->next!=end)
//当p->next的值为end时,表示到链尾
{
if(p->data>q->data)//按照数据域从小到大排序
{
temp=p->data;
p->data=q->data;
q->data=temp;
}
p=q;
q=q->next;
}
end=p;//使end指向每次排序的q所指的结点即尾结点
}
} void print(listnode *head)
{
listnode *p=head->next;
while(p!=NULL){
printf("%d -> ",p->data);
p=p->next;
}
printf("\n");
}
int main()
{
listnode *head=Create();
bubblesort(head);
print(head);
}
3、链表的插入排序(推荐方法)
http://blog.csdn.net/chengtao_xd/article/details/8711451
4、字符串反转的优化
1、交换的地方
2、利用指针比较快
20140903 dynamic_cast和static的区别 链表的冒泡排序和插入排序的更多相关文章
- php self与static的区别
self vs static 用一个demo来直接说明self与static的区别.self示例: <?phpclass Vehicle { protected static $name ...
- 转:C++ 匿名namespace的作用以及它与static的区别
匿名namespace的作用以及它与static的区别 一.匿名namespace的作用在C语言中,如果我们在多个tu(translation unit)中使用了同一个名字做为函数名或者全局变量名,则 ...
- C# 总结const、 readonly、 static三者区别:
总结const. readonly. static三者区别: (有人问我,看似简单,我也没能立刻回答出来,总结一下,分享一下.) const:静态常量,也称编译时常量(compile-time con ...
- c++ dynamic_cast 和 static_cast 的区别
今天在看王道宝典的时候看到dynamic_cast ,一直都没用过,也不了解,今天来总结一下. dynamic_cast 和 static_cast 都可以用来强制转换指针类型,但不同的是dynami ...
- PHP中new self()和new static()的区别
1.new static()是在PHP5.3版本中引入的新特性. 2.无论是new static()还是new self(),都是new了一个新的对象. 3.这两个方法new出来的对象有什么区别呢,说 ...
- PHP中new self()和new static()的区别探究
1.new static()是在PHP5.3版本中引入的新特性. 2.无论是new static()还是new self(),都是new了一个新的对象. 3.这两个方法new出来的对象有什么区别呢,说 ...
- Java中主类中定义方法加static和不加static的区别
Java中主类中定义方法加static和不加static的区别(前者可以省略类名直接在主方法调用(类名.方法),后者必须先实例化后用实例调用) 知识点:1.Getter and Setter 的应用 ...
- php面向对象编程self和static的区别
在php的面向对象编程中,总会遇到 class test{ public static function test(){ self::func(); static::func(); } public ...
- C++ 匿名namespace的作用以及与static的区别
匿名namespace的作用以及它与static的区别 一.匿名namespace的作用 在C语言中,如果我们在多个tu(translation unit)中使用了同一个名字做 为函数名或者全局变量名 ...
随机推荐
- CF560补题
D题:用来对比的vector<long long> b不能被初始化大小成n,因为a里面有n个因子,但是这是可能存在遗漏情况的.如果刚好是遇到实际因子远多于n,那么就会在运行过程中出错. 还 ...
- OSG+Visual Studio2015项目变量设置;
OSG源码经过CMAKE编译后: 1.配置OSG环境变量: 用户变量的PATH中添加路径 C:\OSG\bin系统变量中添加新变量OSG_FILE_PATH为 C:\OSG\data 2.VS新建项目 ...
- C++中的函数重载分析(二)
1,重载与指针: 1,下面的函数指针将保存哪个函数的地址? int func(int x) { return x; } int func(int a, int b) { return a + b; } ...
- opencv中图像的读取,显示与保存1
1.读入图像 用cv2.imread()函数来读取图像,cv2.imread(路径,图像颜色空间)(其中颜色空间默认为BGR彩图) cv2.IMREAD_COLOR:读入一副彩色图像 cv2. ...
- FMDB使用的一点心得:数据库创建、制表、查询等以及image转换成二进制nsdata保存到数据库中
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u012951123/article/details/36871955 <span style= ...
- css颜色单位
/* 用颜色的单词表示不同的颜色:red, green, blue等等 */ p { background-color: red; } /* 用rgb三元色表示,rgb => red, gree ...
- SQL数据库—<1>SQL语言
关系数据库.SQL语言简单.学习软件介绍 SQL:Structured Query Language 结构化查询语言 数据库分为:层次型,网状型,关系型. 关系型数据库:是一个二维表的集合,可以用来存 ...
- Java 枚举(enum)详解
概念: Java1.5发行版本中增加了新的引用类型--枚举类型(enum type).枚举类型是指由一组固定的常量组成合法值的类型.在Java虚拟机中,枚举类在进行编译时会转变成普通的Java类. 创 ...
- day01 python初识、数据类型、流程控制
今日内容大纲:1,计算机基础. cpu,内存,硬盘,操作系统.2,python的发展与应用.3,python的历史. 2008年python同时更新了两个版本 1,python2x python3x ...
- linux随笔-05
shell脚本&定时任务 编写Shell脚本 可以将Shell终端解释器当作人与计算机硬件之间的“翻译官”. Shell脚本命令的工作方式有两种:交互式和批处理. 交互式(Interactiv ...