虚函数定义 [英] Virtual function definition

查看:89
本文介绍了虚函数定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在将我的经验从C转移到C ++,所以我面临着一些继承问题。


这是我的问题:


A级{

//所有分享属性

// ....

虚拟无效开放(无效);

}


B类:公众A {

无效公开(无效);

}


C级:公开A {

void打开(int i);

}


定义B没有问题。但是对于C,有。解决方案

可能是,在父类中设置另一个虚拟,并在B上定义它为

NULL但是这不是我喜欢的。我知道有一个

可能会改变孩子们的返回值,但是我怎么能用函数的参数来实现
呢?


有没有办法呢?


TA。

解决方案



George写道:


大家好,


我正在将我的经验从C转移到C ++,所以我面临一些

继承问题。


这是我的问题:


A级{

//所有分享属性

// ....

虚拟void Open(void);

}


class B:public A {

void Open(void);

}


class C:public A {

void Open(int i);

}


定义B没有问题。但是对于C,有。解决方案

可能是,在父类中设置另一个虚拟,并在B上定义它为

NULL但是这不是我喜欢的。我知道有一个

可能会改变孩子们的返回值,但是我怎么能用函数的参数来实现
呢?


有没有办法做到这一点???



不。在C ++中,当

编译器

选择要调用的函数时,会考虑参数列表(签名)。


所以你的C级知道函数:

void Open(int i);



void Open(); //最好省略void如果它是唯一的参数

继承自A


干杯,

Marc


George写道:


有没有办法做到这一点?



你想要做什么?


您的代码没有特别的问题,它是'只是有点奇怪。

你的_overriding_一个B中的函数和_overwriting_一个函数(实际上是名字中的
)在C中调用A :: Open永远不会调用C ::打开。


如果这就是你想要的,你很好。如果没有,你想要什么?显然

你不能希望用不同的参数覆盖虚拟函数

列表。


Jens


"乔治" < ge ********* @ NOSPAMgmx.netWrote in message

news:Xn ********************* *** @ 213.0.184.81 ...


大家好,


我正在从C中移动我的经验到C ++,所以我面临一些

继承问题。


这是我的问题:


class A {

//所有共享属性

// ....

virtual void Open(void);

}


B级:公开A {

无效公开(无效);

}


class C:public A {

void Open(int i);

}


定义B没有问题。但是C有。解决方案

可能是,在父类中设置另一个虚拟,并在B上定义它为

NULL但是这不是我喜欢的。我知道有一个

可能会改变孩子们的返回值,但是我怎么能用函数的参数来实现
呢?


有没有办法做到这一点???



你为什么要这样做?你能告诉我们你想要解决的问题吗?


虚拟函数的点*不能*派生类能够从他们的基类做任意不同的事情。
该函数应该在基类和每个派生的

类中具有相同的* apparent *行为,即使*实现*可能不同。如果函数

更改其参数或返回类型,则它不再具有相同的明显

行为。


如果找到你自己试着做一些不同的事情,你可能会错误地使用虚拟功能,并且应该重新考虑你的设计。


菲利普


Hi all,

I''m moving my experince from C to C++ and while so I''m facing some
problems with inheritance.

Here is my problem:

class A {
// All share attributes
// ....
virtual void Open(void);
}

class B: public A {
void Open(void);
}

class C: public A {
void Open(int i);
}

There is no problem on defining B. However with C there is. The solution
could be, setting in the parent class another virtual and define it to
NULL on B. But this is not what I like. I know that there is a
possibility of changing the return value on the children, but how can I
do so with the arguments of the function???

Is there any way of doing it???

TA.

解决方案


George wrote:

Hi all,

I''m moving my experince from C to C++ and while so I''m facing some
problems with inheritance.

Here is my problem:

class A {
// All share attributes
// ....
virtual void Open(void);
}

class B: public A {
void Open(void);
}

class C: public A {
void Open(int i);
}

There is no problem on defining B. However with C there is. The solution
could be, setting in the parent class another virtual and define it to
NULL on B. But this is not what I like. I know that there is a
possibility of changing the return value on the children, but how can I
do so with the arguments of the function???

Is there any way of doing it???

Nope. In C++ the argument list (the signature) is considered when the
compiler
chooses which function to call.

So your class C knows to functions:
void Open(int i);
and
void Open(); // better to omit void if it is the only argument
inherited from A

Cheers,
Marc


George wrote:

Is there any way of doing it???

What exactly do you want to do?

There is not a particular problem with your code, it''s just a bit weird.
Your _overriding_ a function in B and _overwriting_ a function (actually
the name) in C. A call to A::Open will never call C::Open.

If that''s what you want, you''re fine. If not, what do you want? Clearly
you can''t hope to override a virtual function with different parameter
lists.

Jens


"George" <ge*********@NOSPAMgmx.netwrote in message
news:Xn************************@213.0.184.81...

Hi all,

I''m moving my experince from C to C++ and while so I''m facing some
problems with inheritance.

Here is my problem:

class A {
// All share attributes
// ....
virtual void Open(void);
}

class B: public A {
void Open(void);
}

class C: public A {
void Open(int i);
}

There is no problem on defining B. However with C there is. The solution
could be, setting in the parent class another virtual and define it to
NULL on B. But this is not what I like. I know that there is a
possibility of changing the return value on the children, but how can I
do so with the arguments of the function???

Is there any way of doing it???

Why do you want to do this? Can you tell us the problem you''re trying to
solve?

The point of a virtual function *not* for derived classes to be able to do
arbitrarily different things from their base class. The function should have
the same *apparent* behaviour in the base class and each of the derived
classes, even though the *implementation* may be different. If the function
changes its arguments or return type, it no longer has the same apparent
behaviour.

If you find yourself trying to do something different, you''re probably
misusing virtual functions and should reconsider your design.

Philip


这篇关于虚函数定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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