Skip to content

Instantly share code, notes, and snippets.

@vpetrigo
Last active June 8, 2018 07:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vpetrigo/bc4493af53dd80ba5a66a54e3322a326 to your computer and use it in GitHub Desktop.
Save vpetrigo/bc4493af53dd80ba5a66a54e3322a326 to your computer and use it in GitHub Desktop.
#include <iostream>
struct Base {
};
struct D1 : Base {
};
struct D2 : Base {
int a{10};
};
struct D3 : D1, D2 {
};
// base указывает на экземпляр Base, соответствующий D1
// нужно вернуть указатель на экземпляр Base, соответсвующий D2
Base * D1BaseToD2Base( Base const * base )
{
/* YOUR IMPLEMENTATION HERE*/
}
int main() {
D3 d3;
Base *ptr = static_cast<D1 *> (&d3);
Base *ptra = NULL;
ptra = D1BaseToD2Base(ptr);
std::cout << static_cast<D2 *>(ptra)->a << std::endl;
return 0;
}
@vitalyisaev2
Copy link

Line std::cout << static_cast<D2 *>(ptra)->a << std::endl; should be replaced with std::cout << static_cast<D2 const*>(ptra)->a << std::endl;, otherwise it won't be compiled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment