I just discovered the following behavior: having an object of type B derived from type A , the final type at construction time A is A , not B This can be seen in the following example:
#include <iostream>
Running this code (compiled with gcc 4.8.5) is as follows:
0x400ae0 0x400ae0 0x400ac0
We see that the type returned by typeid in A::A() is A , not B , and then the final type changes to B
Why?
Is it possible to find out the "real" final type when building the parent class?
My context is as follows:
I have a parent Resource class and several classes that inherit it. I also have a ResourceManager notified by each resource creation and need to know the final type of resource created. What I am doing to avoid code duplication is the following, but it does not work:
class Resource { public: Resource() { ResourceManager::notifyCreation(*this); } ~Resource() { ResourceManager::notifyDestruction(*this); } }; class MyResource : public Resource {
I know that I can execute a notification in each constructor / destructor of children, but it is less reliable (possible error if the resource is instantiated without notifying the manager). Do you have an idea for a workaround?
source share