另一个c ++遗传/虚拟问题 [英] another c++ inheretence/virtual quesion

查看:67
本文介绍了另一个c ++遗传/虚拟问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我有一个简单的问题,但我无法在任何

文献中找到答案。


我有一个基类A,它有一些功能。现在,我的理解

的虚函数意味着:


A类{

virtual void foo()= 0;

void bar(){cout<<" bar"<< endl;}

};


B级:公共A {

void foo(){cout<<"" foo"<< endl;}

void bar(){cout<< ;" bar2"<< endl;}

};


每个派生类A *的B类必须*定义foo或者我得到一个链接器

错误。


另外,如果B类定义了bar,它将从A类调用bar而不是B
即打印出bar而不是bar2


我想要的是在A中定义一个函数,如果它没有定义
B中的
,那么它使用A中的那个如果在B中定义,那么它将使用B中的那个。(但它没有

*有*定义)我该怎么做呢?谢谢!


oliver

Hi. I have a simple question, but I couldnt find the answer in any
literature online.

i have a base class A, that has some functions. now, my understanding
of virtual functions means that in:

class A {
virtual void foo() = 0;
void bar() {cout<<"bar"<<endl;}
};

class B : public A {
void foo() {cout<<"foo"<<endl;}
void bar() {cout<<"bar2"<<endl;}
};

every class B that derives class A *must* define foo or I get a linker
error.

also, if class B defines bar, it will call the bar from class A rather
than B. i.e. print out bar rather than bar2

What i want is to have a function defined in A that if it isnt defined
in B, then it uses the one in A

if it is defined in B, then it uses the one in B. (but it doesnt
*have* to be defined) how do i do this? thanks!

oliver

推荐答案

这在你的例子中已经完成了。由于你在
A中定义了bar(),如果它没有在B中定义,则基类中的版本将被调用

(前提是所有纯虚函数都已定义)你做过的事情)


2005年4月13日星期三20:46:26 -0700,laniik写道:
This is pretty much done in your example. Since you have bar() defined in
A, if it is not defined in B the version in the base class will be called
(provided all pure virtual functions are defined which you have done)

On Wed, 13 Apr 2005 20:46:26 -0700, laniik wrote:
嗨。我有一个简单的问题,但我无法在网上找到任何
文献的答案。

我有一个基类A,它有一些功能。现在,我对虚拟函数的理解意味着:在类A中;
虚拟空虚foo()= 0;
void bar(){cout<< ;" bar"<< endl;}
};

B类:公开A {
void foo(){cout<<" foo"<< endl;}
void bar(){cout<< ;" bar2"<< endl;}
};

每个派生类A *的B类必须*定义foo或者我得到一个链接器
错误。

另外,如果B类定义了bar,它会从A类调用bar而不是B.比如打印出bar而不是bar2

我想要的是在A中定义一个函数,如果它没有在B中定义,那么它使用A中的一个如果它在B中定义,那么它使用B中的那个。(但它没有*有*定义)我该怎么做?谢谢!

oliver
Hi. I have a simple question, but I couldnt find the answer in any
literature online.

i have a base class A, that has some functions. now, my understanding
of virtual functions means that in:

class A {
virtual void foo() = 0;
void bar() {cout<<"bar"<<endl;}
};
class B : public A {
void foo() {cout<<"foo"<<endl;}
void bar() {cout<<"bar2"<<endl;}
};

every class B that derives class A *must* define foo or I get a linker
error.

also, if class B defines bar, it will call the bar from class A rather
than B. i.e. print out bar rather than bar2

What i want is to have a function defined in A that if it isnt defined
in B, then it uses the one in A

if it is defined in B, then it uses the one in B. (but it doesnt
*have* to be defined) how do i do this? thanks!

oliver






这就是我的第一个想法,但是当我编译并测试它时,

致电


B b;

b.bar();


会打印出bar而不是bar2

thats what I first thought, but when i compiled and tested it out,
calling

B b;
b.bar();

would print out "bar" rather than "bar2"


你在什么平台上?


2005年4月13日星期三21 :08:30 -0700,laniik写道:
What platform are you on?

On Wed, 13 Apr 2005 21:08:30 -0700, laniik wrote:
这就是我最初的想法,但是当我编译并测试它时,
致电

B b;
b.bar();

将打印出bar而不是bar2
thats what I first thought, but when i compiled and tested it out,
calling

B b;
b.bar();

would print out "bar" rather than "bar2"






这篇关于另一个c ++遗传/虚拟问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆