简体   繁体   English

C ++继承和重载,一旦重载就调用基本方法

[英]C++ inheritance and overloading, calling a base method once overloaded

Currently I have a class which contains another and provides a wrapper for some of its methods and provides others intact: 目前,我有一个包含另一个类的类,该类为其某些方法提供包装,并完整提供其他类:

class bar {
public:

    int calculate1(int a, int b) {
        return f.calculate1(a, b);
    }

    int calculate2(int a, int b) {
        a ++;
        b ++;
        return f.calculate2(a, b);
    }

private:
    foo f
}

Where foo is a class containing 2 methods calculate1 and calculate2 , both taking 2 int s and returning an int . 其中foo是一个包含2个方法calculate1calculate2 ,它们均采用2 int并返回int

Is there a better way to achieve this with inheritance? 有没有更好的方法可以通过继承来实现呢? It would be nice to be able to get rid of this: 能够摆脱这种情况会很高兴:

    int calculate1(int a, int b) {
        return f.calculate1(a, b);
    }

But this would need to call the very function it is overloading, and I'm not sure how to do that: 但这需要调用它正在重载的功能,而我不确定如何做到这一点:

    int calculate2(int a, int b) {
        a ++;
        b ++;
        return f.calculate2(a, b);
    }

Is it even possible or advisable? 甚至可能还是明智的? Is there another method to be used in this situation? 在这种情况下还有其他方法可以使用吗?

apologies if some of my code is a bit dodgy, as you can tell I'm a bit new to C++, but I hope I got the message across. 道歉,如果我的某些代码有些晦涩,可以告诉我我对C ++有点陌生,但我希望我能理解这些信息。

To call a base class method, use qualification: 要调用基类方法,请使用限定符:

return foo::calculate1(a, b);

Remember to ensure that your base class methods are virtual if you want them to be called polymorphically. 如果要多态调用基类方法,请记住确保它们是virtual

It possible via this way: 通过这种方式可能:

class bar : public foo {
public:

    int calculate2(int a, int b) {        
        return foo::calculate2(a+1, b+1);
    }
};

1. Is there a better way to achieve this with inheritance?` 1.有没有更好的方法可以通过继承来实现呢?

Of course! 当然!

//sg
#include<iostream>
class foo {
public:
    int calculate1(int a, int b) {
        return a * b;
    }

    int calculate2(int a, int b) {
        a ++;
        b ++;
        return a + b;
    }

};
class bar: public foo {
public:

    int calculate1(int a, int b) {
        return foo::calculate1(a, b);
    }

    int calculate2(int a, int b) {
        a++;
        b++;
        return foo::calculate2(a, b);
    }
};
using namespace std;
int main() {
    bar b;
    cout << b.calculate2(1,2) << endl;
    cout << b.calculate1(1,2) << endl;

    return 0;
}

To inherit or compose? 要继承还是撰写? (is inheritance really better here?) (继承真的更好吗?)

2. Is it even possible or advisable?

We have seen that it is possible. 我们已经看到了可能。 As to whether it is advisable or not, semantics of foo and bar * will decide that. 至于是否可取,foo和bar *的语义将决定这一点。 As they stand , I think inheritance looks much better, foo contains the basic calculators, bar needs to use them besides doing something extra. 就目前而言 ,我认为继承看起来好得多,foo包含基本的计算器,除了做一些额外的事情外,bar还需要使用它们。 Of course, this is what I think happens and how you intend things to be, may/not be correct. 当然,这是我认为会发生的事情,以及您打算如何做,可能/不正确。 ?In which case composition may win. 在这种情况下,构图可能会获胜。

As a thumb rule, you can try saying the following two lines out loud : 根据经验法则,您可以尝试大声说出以下两行:

a) bar is a foo (as in Car is a Vehicle) a)bar foo(就像Car是Vehicle一样)

b) bar has a foo (as in Car has an Engine) b)bar 有一个 foo(如Car中有一个Engine)

If a makes more sense, inherit, otherwise compose. 如果一个更有意义,继承,否则构成。 If none of them makes any sense, then you should probably give your design another cup of coffee. 如果它们都没有意义,那么您可能应该给设计再喝一杯咖啡。

Find more reasons here 在这里找到更多原因


* *

Think of foo as a class widget and bar as a specialized widget, say textbox . textbox一样,把foo看作是一个类widget ,把bar当作一个专门的小部件。 Let calculate be a renderer (a function that draws textbox on screen), you do special things to arguments in bar and then call the standard renderer in foo . calculate成为renderer (一个在屏幕上绘制文本框的函数),对bar参数进行特殊处理,然后在foo调用标准渲染器。 In this case, it makes sense to say that bar is a foo . 在这种情况下,可以说bar foo On the other hand, let bar be a constraint solver which takes the arguments, does some calculation and passes them on to a standard la engine foo to do final calculations, then I'd say that bar has a linear algebra engine, foo . 另一方面,让bar是一个约束求解器,它接受参数,进行一些计算,然后将其传递给标准la engine foo进行最终计算,那么我想说bar 有一个线性代数引擎foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM