Created
June 17, 2019 07:52
-
-
Save ppLorins/9134e0c7dfa4f66070a4fcc9b75fc417 to your computer and use it in GitHub Desktop.
subclass pointer convert2 parent class pointer issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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