不是通过引用或指针调用的话,无法产生多态的效果
2013-06-20 22:59
274 查看
#include "stdafx.h"
#include"iostream"
using namespace std;
class A {
public :
virtual void run (){cout<<"1"<<endl;}
};
class B :public A{
public:
void run(){cout<<"2"<<endl;}
};
int f (A &pA){ // int f (A pA)
// pA->run();
pA.run();
return 0;
}
int main(int argc, char* argv[])
{
A d;
B b;
int c=f(b);
return 0;
}
#include"iostream"
using namespace std;
class A {
public :
virtual void run (){cout<<"1"<<endl;}
};
class B :public A{
public:
void run(){cout<<"2"<<endl;}
};
int f (A &pA){ // int f (A pA)
// pA->run();
pA.run();
return 0;
}
int main(int argc, char* argv[])
{
A d;
B b;
int c=f(b);
return 0;
}
相关文章推荐
- (基础)指针的引用调用,通过指针进行数组的输入输出,指针与++
- Java中通过为null的引用调用静态方法
- C/C++—— 在构造函数中调用虚函数能实现多态吗(Vptr指针初始化的过程分析)
- C++ 引用变量(十二)-- & b *b和C的传递指针形参一样效果
- 浅析C++中的this指针 通过空指针(NULL)可以正确调用一些类的成员函数?
- 【bug】对象激活前无法通过调用脚本函数设置变量
- 每个 GROUP BY 表达式必须至少包含一个不是外部引用的列、加载页面时调用Js方法、调用Js文件中的方法
- C# 调用Array.Copy复制的是值,而不是引用,结构也是复制值
- 程序员自定义的一个头文件,怎样通过#include<xxx.h>调用而不是#include"xxx.h"
- 通过URL调用WebService里面的方法,报“因URL意外地以***结束,请求格式无法识别。”解决方案
- C++ 通过null指针调用成员函数 正常
- 用C#通过反射实现动态调用WebService 告别Web引用(转载)
- [转] 通过汇编代码理解成员函数指针并不是指针
- 关于vc++2010中无法通过GetMenu()获得菜单指针
- 无法分析从服务器收到的消息。之所以出现此错误,常见的原因是: 在通过调用 Response.Write() 修改响应时,将启用响应筛选器、HttpModule 或服务器跟踪。
- cons对象/指针/引用,不能调用非const成员函数
- C语言学习之 通过指向函数的指针 实现函数的调用
- 每日总结:每个 GROUP BY 表达式必须至少包含一个不是外部引用的列、加载页面时调用Js方法、调用Js文件中的方法
- 通过指针进行迭代和通过索引进行迭代是否产生相同代码
- apex中无法通过外部sql调用数据库过程(apex安全机制)