简体   繁体   English

在 C++ 中被覆盖后如何调用原始 function?

[英]How to call original function after it's been overridden in C++?

I serialize most of my classes with two functions, read() and write().我用两个函数 read() 和 write() 序列化我的大部分类。 What I would like to do is have the read/write() function of the base class called from the subclasses so that I don't have to repeat the serialization code multiple times.我想做的是从子类调用基本 class 的 read/write() function,这样我就不必多次重复序列化代码。

For example:例如:

class Base
{
public:
   base();
   virtual read(QDataStream&);
   virtual write(QDataStream&);
private:
   int a, b, c, d;
}

class Sub : public Base
{
public:
    Sub();
    read(QDataStream&);
    write(QDataStream&);
private:
    int e, f, g, h;
}

So in this example, i would like the code to read/write a,b,c,d to come from Base.所以在这个例子中,我希望读取/写入 a,b,c,d 的代码来自 Base。 Sub would then call Base::read(QDataStream&) and then add whatever attributes are unique to Sub.然后,Sub 会调用 Base::read(QDataStream&),然后添加 Sub 独有的任何属性。 This way I don't have to repeat the serialization code for each subclass (and possibly forget to do so).这样我就不必为每个子类重复序列化代码(并且可能忘记这样做)。

You can call base-class functions by prepending the function call with the base-class identifier, followed by the scope operator (::).您可以通过在 function 调用前加上基类标识符来调用基类函数,然后是 scope 运算符 (::)。

Like this:像这样:

class Base
{
public:
     virtual void Function();
}

class Foo : public Base
{
public:
     void Function();
     void DoSomething();
}

void Foo::DoSomething()
{
     Base::Function();   // Will call the base class' version of Function().
     Function();         // Will call Foo's version of Function().
}

EDIT: Note removed by request.编辑:请注意删除。

void Sub::read(QDataStream &stream)
{
    Base::read(stream);
    // do Sub stuff here
}

First off - your member functions don't have return types - not valid in C++, your compiler might complain about 'default int' behavior and let it pass, but you should give them return types (I'll use void as a placeholder).首先 - 您的成员函数没有返回类型 - 在 C++ 中无效,您的编译器可能会抱怨“默认 int”行为并让它通过,但您应该给它们返回类型(我将使用void作为占位符) .

You can scope resolve and use the parent class version when you're in a subclass:当您在子类中时,您可以 scope 解析并使用父 class 版本:

void Sub::read(QDataStream& qds) {
    Base::read(qds);

    // do more stuff
}

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

相关问题 C++:如何在不知道派生class的情况下调用虚拟function的派生(覆盖)function? - C++: How to call the derived (overridden) function of a virtual function without knowing the derivative class? 如何在转换为基数 class 后让 C++ 调用重写方法 - How to have C++ call overridden method after a conversion to the base class C ++重写函数未调用 - C++ overridden function not called 在函数调用之后,以下c ++函数中的数组重新分配似乎已被忽略。 - The array reassignment in the following c++ function seems to have been ignored after the function call. 如何将基类 class 的未知子类放入一个数据结构中,并在 C++ 中调用覆盖的基类 class function - How to put unknown child classes of a base class in one data structure and call an overridden base class function in C++ 在C ++中的父级构造函数中调用重写的方法 - Call overridden method in parents constructor in C++ hotpatch后如何调用原始函数 - How to call original function after hotpatch C ++在重写函数中使用基本函数 - C++ Using base function in overridden function 如果方法被覆盖,C ++基类如何在运行时确定? - How can a C++ base class determine at runtime if a method has been overridden? C ++:使用模板时不覆盖函数 - C++: Function not overridden when using templates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM