按照c++之父的经典之作 c++程序设计语言的说法
dynamic_cast<type>()可以把基类的指针转化为子类的指针,代码如下:
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
Person(string name,int age){
this->name=name;
this->age=age;
}
virtual void show(){
cout<<name<<" "<<age<<endl;
}
virtual ~Person(){}
private:
string name;
int age;
};
class Student: public Person{
public:
Student(string name1,int age1,string num1):Person(name1,age1),num(num1){}
void show(){
Person::show();
cout<<num<<endl;
}
~Student(){}
private:
string num;
};
int main(){
Person *p;
Person ss("zhang",22);
p=&ss;
Student *s=dynamic_cast<Student*>(p);
if(s==0){
cout<<"NULL POINTER"<<endl;
}else{
cout<<"IT IS NOT NULL POINTER"<<endl;
}
//s->show();
system("pause");
return 0;
}
以上的代码理论上是可以运行的,但是输出结果为 NULL POINTER,为什么呢?哪位仁兄可以指点一二,哪里出问题了,小弟感激不尽。
typeid()则可以用来判断类型是否一致,typeid().name()可以返回类型,代码如下
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
Person(string name,int age){
this->name=name;
this->age=age;
}
virtual void show(){
cout<<name<<" "<<age<<endl;
}
virtual ~Person(){}
private:
string name;
int age;
};
class Student: public Person{
public:
Student(string name1,int age1,string num1):Person(name1,age1),num(num1){}
void show(){
Person::show();
cout<<num<<endl;
}
~Student(){}
private:
string num;
};
int main()
{
Person p("zhang",22);
Person pp("li",21);
Student ss("wangwu",25,"091150");
if(typeid(p)==typeid(pp)){
cout<<"p,pp类型相同"<<endl;
}else{
cout<<"p,pp类型不相同"<<endl;
}
if(typeid(p)==typeid(ss)){
cout<<"p,ss类型相同"<<endl;
}else{
cout<<"p,ss类型不相同"<<endl;
}
int a=100;
cout<<"a的类型为"<<typeid(a).name()<<endl;
cout<<"p的类型为"<<typeid(p).name()<<endl;
system("pause");
return 0;
}
欢迎回帖,第一个代码问题出在哪里,为什么s为空,朋友你知道吗?
问题得到了解决:
主函数改成
int main(){
Person *p = new Student("zhang",22,"091150");
Student *s=dynamic_cast<Student*>(p);
if(s==0){
cout<<"NULL POINTER"<<endl;
}else{
cout<<"IT IS NOT NULL POINTER"<<endl;
s->show();
}
system("pause");
return 0;
}
分享到:
相关推荐
TypeID=1>文章1</a><br><br>把新闻显示在主页或其它页面上<br><script type="text/javascript" src="/Files/ShowFileItems.aspx?Num=8&TypeID=1&ShowTime=YES"></script> <br>Num=数字 你要显示的信息条数<br>TypeID...
triangle* 七p = dynamic_cast<triangle*>(sh); 它自增。如果可以控制类的源代码并可以修改它,当然可以这样做。下面这个例子用来计算 s h a p e的个数,它用了静态数据成员和动态映射两种方法: 384 C + +编程思想 ...
<td width="202" height="84"><a href="<%=rswebsite("web_firstpage")%>"><img src="<%=rswebsite("web_logo")%>" alt="logo" name="logo" width="200" height="60" border="0" id="logo" /></a></td> <td width=...
连接数据库,对实体的操作 <form action="${... <option value="${type.typeid}"> ${type.typename} </option> </c:forEach> </select> <p><input type="submit" value="提交" /> </form>
<td><input name="specialName" type="text" id="specialName" size="30" title="专辑名"></td> </tr> <tr> <td align="center">歌曲文件:</td> <td><input name="fileURL" type="text" id="fileURL" size="30...
第一章 一、选择题 1.B; (typedef ,typeid ,... cout<<"字符输出:"<<(int)a<<","<<(int)b<< endl; cout<<"ASCII码输出:"<<(char)ascii_2<<(char)ascii_1<<","; cout<<(char)ascii_1<<(char)ascii_1<< endl; }
1.这个数形菜单基于java+bean+tomcat+sql2000写的<br>2.ConnDB.properties是数据库配置文件,可参照ConnDB.java进行修改。<br>3.ChStr.java处理字符。<br>4.把teee1.jsp放在tomcat--》webapps下配置一下就好了。<br>...
<option value="<%=rs_city.getInt("ID")%>" <%if(rs_city.getInt("ID")==SelectID_C){%>selected<%}%>><%=rs_city.getString("Name")%> </option> <% } while (rs_city.next()); %> </select> </div> <%} ...
必须包含 <typeinfo> 标头文件才能使用 type_info 类。 type_info 类的接口是: class type_info { public: virtual ~type_info(); size_t hash_code() const _CRTIMP_PURE bool operator==(con
asm auto bad_cast bad_typeid bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum except explicit extern false finally float for friend goto if ...
<script src="js/jquery-ui-1.8.2.custom.min.js" type="text/javascript"> </script> <script src="js/jquery.layout.js" type="text/javascript"></script> <script src="js/i18n/grid.locale-en.js" type="text/...
dynamic_cast else enum except explicit extern false finally float for friend goto if inline int long mutable namespace new operator private protected public register reinterpret_cast return short ...
#include <iostream> #include <vector> using namespace std; int main() { vector<int> ivec; const vector<int> cv; auto it1 = ivec.begin(); auto it2 = cv.begin(); cout<<typeid
<div class=switchBox id=timeList typeId=time> <table cellpadding=0 cellspacing=0> <tr> <td class=switch_box_l></td> <td class=switch_box_c> <span class=abs> <i class=switchBtn-l></
document.write('<script type="text/javascript" >BAIDU_CLB_SLOT_ID = "288992";</script>'); document.write(ojs);} if (code=="top210x90_2"){ document.write('<script type="text/javascript" >BAIDU_CLB_SLOT...
/// <param name="TypeID">类别编号</param> /// <param name="lblTypeName">显示类别名称的标签ID</param> private void Bind(Repeater rep,string TypeID,Label lblTypeName) { string sqlString = "select ...
<option value=-1>-请选择类型-</option> <option value=grade>兑换等级</option> <option value=money>兑换现金</option> <option value=gift>兑换礼物</option> </select> <tr id=grade></tr> <tr id=money>...
<procedure name> [(<parameter list>)] IS|AS <local variable declaration> BEGIN <executable statements> [EXCEPTION <exception handlers>] END; */ create or replace procedure proc_getproductNambyId...
<td valign="top" bgcolor="#FFFFFF"><table width="100%" height="509" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" class="tableBorder_gray"> <tr> <td align="left" ...