Can I call a base class's virtual function if I'm overriding it?
The C++ syntax is like this:
class Bar : public Foo {
// ...
void printStuff() {
Foo::printStuff(); // calls base class' function
}
};
Can I call a virtual function that is overridden from main()?
can I print print base class with the object
d
of the derived class with just changingmain()
Yes, you can. You have to explicitly use the base class in the call.
bptr->base::print();
You can also use d
directly.
d.base::print();
Virtual function of derived class calls that of base class
The commented out code is right.
Widget::resize();
Your substitute code is wrong.
Widget* th = static_cast<Widget*>(this);
th->resize();
Think about it: You are calling a virtual function through a pointer to the base class. What happens when you do that with any virtual function? It calls the most derived version. In other words, endless recursion.
Requiring overridden virtual functions to call base implementations
The way its done is the base class method is not virtual and calls a protected virtual method.
Of course, that only handles one level.
In your particular situation, a large amount of infrastructure can make it work, but it's not worth it.
The typical response is to add a comment
// Always call base class method
When is an overridden virtual function not called
A base class method might be called instead of a derived class method when the base class destructor calls a virtual function. In this case, the derived class is already destroyed and no virtual method can be called. (More info on this question).
Back to your question:
From MFC code, dbcore.cpp using VS 2019 (14.22.27905):
CRecordset::FreeRowset()
calling DoBulkFieldExchange
, and looks like that is some cases FreeRowset()
is called by the destructor of CRecordset
.
This is the comment from CRecordset::FreeRowset
code.
Calling virtual function,
DoBulkFieldExchange
, here is bad
becauseClose
thenFreeRowset
may get called from destructor.
There is no simple choice however ifRFX_Bulk
functions do
a memory allocation. The net result is that users MUST call
Close
explicitly
(rather than relying on destructor) if using multi row fetches,
otherwise they will get a memory leak. If rowset already allocated,
delete old rowset buffers
Overriding only some virtual funtion of base class with the same name
Just add a using declaration inside derived class as shown below. In particular, a using declaration for a base-class member function (like foo
) adds all the overloaded instances of that function to the scope of the derived class. Now you can override the version that takes an argument of type int
and use the implementation of others that were added in the derived class' scope using the using declaration.
class Derived : public Base
{
public:
//added this using declaration
using Base::foo;
void foo() override {
std::cout << "Derived::foo()" << std::endl;
}
};
Working demo
The output of the above modified program is as you wanted:
Base::foo()
Base::foo(int i)
Derived::foo()
Base::foo(int i)
Calling the overriding function through a reference of base class
But what confuses me is that a instance of derived class could be assigned to a reference of the base class. And calling the overriding function through the said reference finally invoking the function of the derived class other than the base class.
It is indeed exactly like that.
If a member function is virtual
in a class, then calling it through a pointer-or-reference to that class will result in a dynamic dispatch, so if the actual object is of a derived class which overrides that function (btw, it neededn't declare it virtual
because it's already virtual; and it'd better delcare it override
, so that it errors out if it doesn't really override, e.g. because you mistyped the name), than that override will be called.
Tha page linked above should sufficient to shed light on this as well as other doubts.
Related Topics
Undefined Behavior and Sequence Points Reloaded
Using Opencv and Svm With Images
Is There a Replacement For Unistd.H For Windows (Visual C)
What Is the Fastest Way to Transpose a Matrix in C++
How Does #Include ≪Bits/Stdc++.H≫ Work in C++
How to Use the Qt'S Pimpl Idiom
Why Can't I Use Float Value as a Template Parameter
Have You Used Any of the C++ Interpreters (Not Compilers)
How Can a Windows Service Execute a Gui Application
Create Random Number Sequence With No Repeats
How to Implement an Autonomous 'Self' Member Type in C++
Variable Initialization in C++
Selectively Disable Gcc Warnings For Only Part of a Translation Unit
Why Do C++11-Deleted Functions Participate in Overload Resolution