默认参数和继承 [英] default parameters and inheritance

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

问题描述

亲爱的C ++程序员,


我有2个班级

A和B

虚拟功能

虚拟长A ::保存();

虚拟长B:保存(bool b = true);


现在,如果我有指向B对象的指针A *:


B b;

A * p =& b;

p->保存();


很遗憾A :: Save()被调用,但我希望B :: Save()是在这种情况下,B :: Save()的默认参数是没有意义的 - 但

编译器没有错误。


???


问候

恩斯特


Dear C++ programmers,

I have 2 classes
A and B
with virtual functions
virtual long A::Save();
virtual long B::Save(bool b = true);

Now, if I have a Pointer A * which points to a B Object:

B b;
A * p = & b;
p->Save();

Unfortunately A::Save() is called but I wanted B::Save() to be called.
In this case, the default parameter of B::Save() is senseless - but the
compiler gives no error.

???

Greetings
Ernst


推荐答案

Ernst Murnleitner写道:
Ernst Murnleitner wrote:
亲爱的C ++程序员,

我有2个班级
A和B
带虚拟功能
虚拟长A :: Save();
虚长B:保存(bool b = true);

现在,如果我有一个指针A *指向B对象:

B b;
A * p =& b;
p-> Save();

不幸的是调用了A :: Save()但是我想要调用B :: Save()。
在此例如,B :: Save()的默认参数是没有意义的 - 但
编译器没有错误。
Dear C++ programmers,

I have 2 classes
A and B
with virtual functions
virtual long A::Save();
virtual long B::Save(bool b = true);

Now, if I have a Pointer A * which points to a B Object:

B b;
A * p = & b;
p->Save();

Unfortunately A::Save() is called but I wanted B::Save() to be called.
In this case, the default parameter of B::Save() is senseless - but the
compiler gives no error.




这正是语言指定的内容。对于B :: Save方法

要调用你需要有一个B * p;即指向B对象的指针。


但是,理论上你可以这样做:


B级

{

.....

虚拟长期保存(){保存(真实); }

虚拟长期保存(bool b);


};


现在你得到你所期望的。这里我们重载了Save()并且制作了

它做你期望的p-> Save()来做。


G



This is exactly what the language specifies. For the "B::Save" method
to be called you need to have a "B * p;" i.e. a pointer to a B object.

However, you could theoretically do this:

class B
{
.....
virtual long Save() { Save( true ); }
virtual long Save(bool b);

};

Now you get what you expect. Here we have overloaded "Save()" and made
it do what you expect p->Save() to do.

G


" Ernst Murnleitner" < mu*@awite.de>写了...
"Ernst Murnleitner" <mu*@awite.de> wrote...
亲爱的C ++程序员,

我有2个班级A / B
带虚拟功能
虚拟长A: :Save();
虚拟长B ::保存(bool b = true);


B ::保存不是A :: Save的重写。他们有不同的

类型。默认参数值不是函数的一部分

类型。

现在,如果我有一个指向B对象的指针A *:
B b;
A * p =& b;
p-> Save();

不幸的是调用了A :: Save()但是我想要调用B :: Save()。


你必须让B :: Save成为A :: Save的替代品。在

命令中,你需要使它们成为相同的类型。

要么给A ::保存参数或从
$ b $中删除参数b B :: Save。

在这种情况下,B :: Save()的默认参数是没有意义的 - 但
编译器没有错误。
Dear C++ programmers,

I have 2 classes
A and B
with virtual functions
virtual long A::Save();
virtual long B::Save(bool b = true);
B::Save is not an overrider for A::Save. They have different
types. Default argument values are not part of the function
type.

Now, if I have a Pointer A * which points to a B Object:

B b;
A * p = & b;
p->Save();

Unfortunately A::Save() is called but I wanted B::Save() to be called.
You have to make B::Save to be the overrider of A::Save. In
order to accomplish that you need to make them the same type.
Either give A::Save the argument or remove the argument from
B::Save.
In this case, the default parameter of B::Save() is senseless - but the
compiler gives no error.




这应该对你有所帮助:


A级{

公开:

虚拟长期保存( );

};


B级{

长期保存(bool b); //不是虚拟和私人

公共:

虚拟长期保存(){this->保存(true); }

};


int main(){

B b;

A * p =& b;

p-> Save(); //将调用B :: Save(),然后调用

// B :: Save(bool)

}


HTH


Victor



This should help you:

class A {
public:
virtual long Save();
};

class B {
long Save(bool b); // not virtual and private
public:
virtual long Save() { this->Save(true); }
};

int main() {
B b;
A *p = &b;
p->Save(); // will call B::Save(), which in turn will call
// B::Save(bool)
}

HTH

Victor




" Ernst Murnleitner" < mu*@awite.de>在消息新闻中写道:是************ @ ID-130107.news.dfncis.de ...

"Ernst Murnleitner" <mu*@awite.de> wrote in message news:be************@ID-130107.news.dfncis.de...
亲爱的C ++程序员,
我有2个班级
A和B
虚拟功能
虚拟长A :: Save();
虚拟长B ::保存(bool b = true);

现在,如果我有一个指向B对象的指针A *:

B b;
A * p =& b;
p-> Save();

不幸的是调用了A :: Save()但是我想要调用B :: Save()。
在此例如,B :: Save()的默认参数是没有意义的 - 但
编译器没有错误。
Dear C++ programmers,

I have 2 classes
A and B
with virtual functions
virtual long A::Save();
virtual long B::Save(bool b = true);

Now, if I have a Pointer A * which points to a B Object:

B b;
A * p = & b;
p->Save();

Unfortunately A::Save() is called but I wanted B::Save() to be called.
In this case, the default parameter of B::Save() is senseless - but the
compiler gives no error.




B :: Save(bool)没有''重写A :: Save。这是不可能发生的事情。

。如果你想要覆盖的话,你必须匹配参数列表。

此外,派生类中的函数隐藏基类定义所以

B :: Save(bool b = true)可能没有意义。


编译器没有发出错误,因为没有错误。



B::Save(bool) doesn''t override A::Save. There''s no way that this is going
to happen. You must match the parameter list if you want things to override.
Further, a function in the derived class hides the base class definitions so
B::Save(bool b = true) is possibly not senseless.

The compiler issues no error because there is no error.


这篇关于默认参数和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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