Created
October 20, 2016 23:32
-
-
Save trevnorris/58cc900045806611777afc3c845a822e to your computer and use it in GitHub Desktop.
log value from a Base class when multi-inheritance is involved
This file contains 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 <cstdio> | |
#include <type_traits> // std::remove_reference | |
#ifdef USE_TR1_TYPE_TRAITS | |
template <typename T> using remove_reference = std::tr1::remove_reference<T>; | |
#else | |
template <typename T> using remove_reference = std::remove_reference<T>; | |
#endif | |
class Foo { | |
public: | |
template <class Base> | |
static Base* GetHandle(void* ptr) { | |
Base* handle; | |
handle = static_cast<typename remove_reference<decltype(*handle)>::type*>(ptr); | |
// I'd like to log a value from Base, but that value only exists on | |
// class instances that have Bar in their inheritance chain. | |
// There any way to do that? | |
return handle; | |
} | |
void printStuff() { | |
fprintf(stderr, "printStuff\n"); | |
} | |
protected: | |
virtual ~Foo() { } | |
}; | |
class Bar { | |
public: | |
void ImHere() { | |
fprintf(stderr, "Bar\n"); | |
} | |
protected: | |
virtual ~Bar() { } | |
}; | |
class Baz : public Foo { | |
public: | |
void printMe() { | |
fprintf(stderr, "Baz\n"); | |
} | |
private: | |
~Baz() { } | |
}; | |
class Bam : public Bar, public Foo { | |
public: | |
void printMe() { | |
fprintf(stderr, "Bam\n"); | |
} | |
private: | |
~Bam() { } | |
}; | |
int main() { | |
void* baz = new Baz(); | |
Foo::GetHandle<Baz>(baz)->printMe(); | |
void* bam = new Bam(); | |
Foo::GetHandle<Bam>(bam)->ImHere(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment