Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ppLorins/9134e0c7dfa4f66070a4fcc9b75fc417 to your computer and use it in GitHub Desktop.
Save ppLorins/9134e0c7dfa4f66070a4fcc9b75fc417 to your computer and use it in GitHub Desktop.
subclass pointer convert2 parent class pointer issue
#include<iostream>
class CPartialBase {
public:
CPartialBase() {
std::cout << "CPartialBase this:" << this << std::endl;
}
virtual void funcPart() = 0;
};
class CPartialDerive: public CBase1,public CPartialBase {
public:
CPartialDerive() {
std::cout << "CPartialDerive this:" << this << std::endl;
}
virtual void funcPart() override {
std::cout << "funcPart called,this:" <<this << std::endl;
}
};
int main(int argc, char** argv){
CPartialDerive * _pd = new CPartialDerive();
CPartialBase* _pb = (CPartialBase*)_pd;
void * _p_tmp = _pd;
CPartialBase* _pbx = (CPartialBase*)_p_tmp;
/*This will causing the runtime error:
'Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.This
is usually a result of calling a function declared with one calling convention with a function
pointer declared with a different calling convention.'
This is because _pbx didn't point to the 'CPartialBase' part of the 'CPartialDerive' object. */
//_pbx->funcPart();
//And this simply can't compile.
//CPartialBase* _pby = dynamic_cast<CPartialBase*>(_p_tmp);
_pb->funcPart();
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment