父类指针,虚函数

lingling1989r 2008-12-21 09:31:17
关于虚函数,学过和没学一样,这里,我想这么写,不知道为什么会出错??帮忙看下吧,对父类指针的指向函数不熟练,感谢能给详细说明的,

#include <iostream>
using namespace std;

class statement
{
public:
virtual void create_();
};

class state_read:public statement
{
void create_(int a){cout<<a<<endl;}
};

int main()
{
statement *p;
p=new state_read;
p->create_(3);
return 0;
}

...全文
216 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangask00 2008-12-22
  • 打赏
  • 举报
回复
函数定义要一模一样
并且你在基类把它实现吧 这样就能跟踪进去了
其他的都没什么问题
duwangduwa 2008-12-22
  • 打赏
  • 举报
回复
参数相同的用重载(虚函数),参数不同直接多态就行(不用虚函数)。
ChamPagneZ 2008-12-22
  • 打赏
  • 举报
回复
恩,你的问题是,不了解编译器的编译机制.
编译阶段,只看静态类型.
sallan 2008-12-22
  • 打赏
  • 举报
回复
虚函数的一个重要组成部分便是override
看一看有帮助
范振勇 2008-12-22
  • 打赏
  • 举报
回复
区分override和overload
看这篇文章吧 http://liubin.itpub.net/post/325/15623
rabbii 2008-12-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fengxuxing 的回复:]
C/C++ code#include"stdafx.h"#include<iostream>usingnamespacestd;classstatement
{public:virtualvoidcreate_(inta)=0;//参数表必须一直,如果不实现,则设置为纯虚函数};classstate_read:publicstatement
{voidcreate_(inta){cout<<a<<endl;}
};intmain()
{
statement*p;
p=newstate_read;
p->create_(3);return0;
}
[/Quote]
overwrite得是一样的定义才行。而且要不是把父类的设置成纯虚的,就必须提供他的实现,哪怕只是{}都可以。
bfhtian 2008-12-21
  • 打赏
  • 举报
回复
看看覆盖和隐藏的区别,在网上搜下,很多的
bitwwzhang130 2008-12-21
  • 打赏
  • 举报
回复
使用函数摸板

xiaoyisnail 2008-12-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lingling1989r 的回复:]
追问一句:基类 和子类的参数表一定要相同么??
[/Quote]

如果是要overwrite虚函数的话参数表必须一样
wap21 2008-12-21
  • 打赏
  • 举报
回复
up
lingling1989r 2008-12-21
  • 打赏
  • 举报
回复
追问一句:基类 和子类的参数表一定要相同么??
lingling1989r 2008-12-21
  • 打赏
  • 举报
回复
我的那个statement 类有几个子类,他们的create()的参数表都不一样,那我这个基类的参数表要怎么弄啊,不写不行么??
Qlaiaqu 2008-12-21
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include <iostream>
using namespace std;

class statement
{
public:
virtual void create_(int a)=0; //参数表必须一直,如果不实现,则设置为纯虚函数
};

class state_read:public statement
{
void create_(int a){cout <<a <<endl;}
};

int main()
{
statement *p;
p=new state_read;
p->create_(3);
return 0;
}
artwl_cn 2008-12-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 lingling1989r 的帖子:]
关于虚函数,学过和没学一样,这里,我想这么写,不知道为什么会出错??帮忙看下吧,对父类指针的指向函数不熟练,感谢能给详细说明的,

#include <iostream>
using namespace std;

class statement
{
public:
virtual void create_();
};

class state_read:public statement
{
void create_(int a){cout < <a < <endl;}
};

int main()
{
statement *p;
p=new state_read;
p->create_(…
[/Quote]
楼主两个类中的函数只是名子相同,参数都不同,肯定看达不到效果。
3楼正解
gccdy 2008-12-21
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

class statement
{
public:
virtual void create_(int i)=0;
};

class state_read:public statement
{
public:
void create_(int a){cout <<a <<endl;}
};

int main()
{
statement *p;
p=new state_read();
p->create_(3);
return 0;
}

elegant87 2008-12-21
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

class statement
{
public:
virtual void create_(int a){}; //把基类的这个函数补全
};

class state_read:public statement
{
public:
void create_(int a){cout <<a <<endl;} //从statement类继承过来的!
};

int main()
{
statement *p;
p=new state_read;
p->create_(3);
system("pause");
return 0;
}
xiaoyisnail 2008-12-21
  • 打赏
  • 举报
回复
派生类要overwrite的基类的虚函数在派生类里要有一样的签名
gccdy 2008-12-21
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

class statement
{
public:
virtual void create_(int);
};

class state_read:public statement
{
void create_(int a){cout < <a < <endl;}
};

int main()
{
statement *p;
p=new state_read;
p->create_(3);
return 0;
}

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧