How to call private virtual function outside class in C ++

Use virtual function pointers outside the class to skip the limitation of private, EG:

#include<iostream>
using namespace std;

class B
{
private:
	virtual void func()
	{
		cout << "func" << endl;
	}

};

int main()
{
	B b;
	long *p1 = (long *)&b;
	long *p2 = (long *)(*p1);
	typedef void(*Func)(void);
	Func func = (Func)p2[0];
	func();

	return 0;
}

The above code personally speculates that the call of the virtual function does not need to pass in this pointer like the ordinary member function in the class (this view may not be correct)

Published 123 original articles · praised 31 · 90,000 views +

Guess you like

Origin blog.csdn.net/qq_40794602/article/details/103914942