虚函数继承过来还是虚函数,具体请看

tangcat123 2013-05-10 04:49:46
#include <IOSTREAM.H>
//基类
class CBase
{
int x;
public:
CBase(int n) {x=n;}
~CBase(){}
virtual void SetX(int n) {x=n;}
virtual int GetX() {return x;}
};

//派生类
class CDerive : public CBase
{
int x;
public:
CDerive(int n1,int n2):CBase(n1)
{
x=n2;
}
~CDerive() {}
void SetX(int n) {x=n;}
int GetX() {return x;}
};


我想问的是继承的话应该把父类的函数继承过来啊。
那么这里CDerive实际上有两个SetX么,一个自己的一个基类的?
那自己的这个SetX是虚函数么?
虚函数表我好像搞清楚了,忽然又在这迷糊了。。。

难道我应该这样假设,c++会有一种机制自动把和虚函数同名的函数放在虚函数表中?
我放在虚函数表中的应该是CDerive里的SetX是么?
有点迷糊 大神指点
...全文
631 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaea2 2015-06-15
  • 打赏
  • 举报
回复

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
 
using namespace std;
 
//基类
class CBase
{
    int x;
public:
    CBase(int n) {x=n;}
    ~CBase(){}
    virtual void SetX(int n) {x=n;cout<<"CBase"<<endl;}
    virtual int GetX() {return x;}
};
 
//派生类
class CDerive : public CBase
{
    int x;
public:
    CDerive(int n1,int n2):CBase(n1)
    {
        x=n2;
    }
    ~CDerive() {}
    void SetX(int n) {x=n;cout<<"CDerive"<<endl;}
    int GetX() {return x;}
};
int main()
{
    CBase *pb = new CDerive(1, 2);
    pb->SetX(3);
    pb->CBase::SetX(4);
}
gaea2 2015-06-15
  • 打赏
  • 举报
回复
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <iostream> using namespace std; //基类 class CBase { int x; public: CBase(int n) {x=n;} ~CBase(){} virtual void SetX(int n) {x=n;cout<<"CBase"<<endl;} virtual int GetX() {return x;} }; //派生类 class CDerive : public CBase { int x; public: CDerive(int n1,int n2):CBase(n1) { x=n2; } ~CDerive() {} void SetX(int n) {x=n;cout<<"CDerive"<<endl;} int GetX() {return x;} }; int main() { CBase *pb = new CDerive(1, 2); pb->SetX(3); pb->CBase::SetX(4); }
gaea2 2015-06-15
  • 打赏
  • 举报
回复
#include <IOSTREAM.H> //基类 class CBase { int x; public: CBase(int n) {x=n;} ~CBase(){} virtual void SetX(int n) {x=n;} virtual int GetX() {return x;} }; //派生类 class CDerive : public CBase { int x; public: CDerive(int n1,int n2):CBase(n1) { x=n2; } ~CDerive() {} void SetX(int n) {x=n;} int GetX() {return x;} };
hugett 2013-05-10
  • 打赏
  • 举报
回复
引用 8 楼 luoxuechengbing 的回复:
[quote=引用 3 楼 hugett 的回复:] 你的SetX,GetX都是虚函数。。子类中跟基类虚函数同名且形参表一样的函数都默认为虚函数。。不用加virtual的。。 基类的版本的确继承过来了。。不过如果你用基类指针或引用调用的时候会动态绑定派生类版本而已。。

#include <iostream>

using namespace std;

//基类
class CBase
{
    int x;
public:
    CBase(int n) {x=n;}
    ~CBase(){}
    virtual void SetX(int n) {x=n;cout<<"CBase"<<endl;}
    virtual int GetX() {return x;}
};

//派生类
class CDerive : public CBase
{
    int x;
public:
    CDerive(int n1,int n2):CBase(n1)
    {
        x=n2;
    }
    ~CDerive() {}
    void SetX(int n) {x=n;cout<<"CDerive"<<endl;}
    int GetX() {return x;}
};
int main()
{
	CBase *pb = new CDerive(1, 2);
	pb->SetX(3);
	pb->CBase::SetX(4);
}
HI~ 又看到你咯。 趁着这个时候问你个问题啊。 如果 基类 class A有个 虚函数 virutal void Set(); 派生类 class B:public A{ void Set() //也是虚函数,有没办法让他成为 非虚函数? 是不是设置成 静态函数就行了? }; [/quote] 没有办法的。。就算你定义了个同名的静态函数,你用基类指针或引用调用set,还是会调用继承来那个虚函数。。而且静态函数是没有办法调用数据成员的。。
  • 打赏
  • 举报
回复
引用 9 楼 tangcat123 的回复:
[quote=引用 3 楼 hugett 的回复:] 你的SetX,GetX都是虚函数。。子类中跟基类虚函数同名且形参表一样的函数都默认为虚函数。。不用加virtual的。。 基类的版本的确继承过来了。。不过如果你用基类指针或引用调用的时候会动态绑定派生类版本而已。。

#include <iostream>

using namespace std;

//基类
class CBase
{
    int x;
public:
    CBase(int n) {x=n;}
    ~CBase(){}
    virtual void SetX(int n) {x=n;cout<<"CBase"<<endl;}
    virtual int GetX() {return x;}
};

//派生类
class CDerive : public CBase
{
    int x;
public:
    CDerive(int n1,int n2):CBase(n1)
    {
        x=n2;
    }
    ~CDerive() {}
    void SetX(int n) {x=n;cout<<"CDerive"<<endl;}
    int GetX() {return x;}
};
int main()
{
	CBase *pb = new CDerive(1, 2);
	pb->SetX(3);
	pb->CBase::SetX(4);
}
分给你了。。。。。。 楼下有个你的粉丝呢,我准备结贴了 你把他的问题回答了呗。。。。。。。。[/quote] O(∩_∩)O哈哈~,谢谢让我搭顺风车。~!
tangcat123 2013-05-10
  • 打赏
  • 举报
回复
引用 3 楼 hugett 的回复:
你的SetX,GetX都是虚函数。。子类中跟基类虚函数同名且形参表一样的函数都默认为虚函数。。不用加virtual的。。 基类的版本的确继承过来了。。不过如果你用基类指针或引用调用的时候会动态绑定派生类版本而已。。

#include <iostream>

using namespace std;

//基类
class CBase
{
    int x;
public:
    CBase(int n) {x=n;}
    ~CBase(){}
    virtual void SetX(int n) {x=n;cout<<"CBase"<<endl;}
    virtual int GetX() {return x;}
};

//派生类
class CDerive : public CBase
{
    int x;
public:
    CDerive(int n1,int n2):CBase(n1)
    {
        x=n2;
    }
    ~CDerive() {}
    void SetX(int n) {x=n;cout<<"CDerive"<<endl;}
    int GetX() {return x;}
};
int main()
{
	CBase *pb = new CDerive(1, 2);
	pb->SetX(3);
	pb->CBase::SetX(4);
}
分给你了。。。。。。 楼下有个你的粉丝呢,我准备结贴了 你把他的问题回答了呗。。。。。。。。
  • 打赏
  • 举报
回复
引用 3 楼 hugett 的回复:
你的SetX,GetX都是虚函数。。子类中跟基类虚函数同名且形参表一样的函数都默认为虚函数。。不用加virtual的。。 基类的版本的确继承过来了。。不过如果你用基类指针或引用调用的时候会动态绑定派生类版本而已。。

#include <iostream>

using namespace std;

//基类
class CBase
{
    int x;
public:
    CBase(int n) {x=n;}
    ~CBase(){}
    virtual void SetX(int n) {x=n;cout<<"CBase"<<endl;}
    virtual int GetX() {return x;}
};

//派生类
class CDerive : public CBase
{
    int x;
public:
    CDerive(int n1,int n2):CBase(n1)
    {
        x=n2;
    }
    ~CDerive() {}
    void SetX(int n) {x=n;cout<<"CDerive"<<endl;}
    int GetX() {return x;}
};
int main()
{
	CBase *pb = new CDerive(1, 2);
	pb->SetX(3);
	pb->CBase::SetX(4);
}
HI~ 又看到你咯。 趁着这个时候问你个问题啊。 如果 基类 class A有个 虚函数 virutal void Set(); 派生类 class B:public A{ void Set() //也是虚函数,有没办法让他成为 非虚函数? 是不是设置成 静态函数就行了? };
  • 打赏
  • 举报
回复
虚函数是会继承的,在基类中是虚函数,那在派生类中也是虚函数。
quickSort 2013-05-10
  • 打赏
  • 举报
回复
http://blog.csdn.net/hello_world_2012/article/details/8757533 我总结的,欢迎指教!
就是那个党伟 2013-05-10
  • 打赏
  • 举报
回复
引用 3 楼 hugett 的回复:
你的SetX,GetX都是虚函数。。子类中跟基类虚函数同名且形参表一样的函数都默认为虚函数。。不用加virtual的。。 基类的版本的确继承过来了。。不过如果你用基类指针或引用调用的时候会动态绑定派生类版本而已。。

#include <iostream>

using namespace std;

//基类
class CBase
{
    int x;
public:
    CBase(int n) {x=n;}
    ~CBase(){}
    virtual void SetX(int n) {x=n;cout<<"CBase"<<endl;}
    virtual int GetX() {return x;}
};

//派生类
class CDerive : public CBase
{
    int x;
public:
    CDerive(int n1,int n2):CBase(n1)
    {
        x=n2;
    }
    ~CDerive() {}
    void SetX(int n) {x=n;cout<<"CDerive"<<endl;}
    int GetX() {return x;}
};
int main()
{
	CBase *pb = new CDerive(1, 2);
	pb->SetX(3);
	pb->CBase::SetX(4);
}
还是看例子比较说明问题,文字好枯燥的说
sophy520eangel 2013-05-10
  • 打赏
  • 举报
回复
子类继承超类的virtual方法并不需要显示的加virtual修饰的 一类一虚表~ 子类有着自己的vPtr指向虚表
hugett 2013-05-10
  • 打赏
  • 举报
回复
你的SetX,GetX都是虚函数。。子类中跟基类虚函数同名且形参表一样的函数都默认为虚函数。。不用加virtual的。。 基类的版本的确继承过来了。。不过如果你用基类指针或引用调用的时候会动态绑定派生类版本而已。。

#include <iostream>

using namespace std;

//基类
class CBase
{
    int x;
public:
    CBase(int n) {x=n;}
    ~CBase(){}
    virtual void SetX(int n) {x=n;cout<<"CBase"<<endl;}
    virtual int GetX() {return x;}
};

//派生类
class CDerive : public CBase
{
    int x;
public:
    CDerive(int n1,int n2):CBase(n1)
    {
        x=n2;
    }
    ~CDerive() {}
    void SetX(int n) {x=n;cout<<"CDerive"<<endl;}
    int GetX() {return x;}
};
int main()
{
	CBase *pb = new CDerive(1, 2);
	pb->SetX(3);
	pb->CBase::SetX(4);
}
ouyh12345 2013-05-10
  • 打赏
  • 举报
回复
那么这里CDerive实际上有两个SetX么,一个自己的一个基类的? 就一个啊,从父类继承过来的
always_learn 2013-05-10
  • 打赏
  • 举报
回复
一个,一日为虚终生为虚

64,648

社区成员

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

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