Following another post I am trying to implement a minimal, working, C++ application to understand how to implement the progressive disclosure pattern. Here is what I have written so far:
#include <iostream>
using std::cout;
using std::endl;
struct IExtBase;
struct IBase
{
virtual void basicMethod() = 0;
virtual IExtBase& ext() = 0;
};
struct IExtBase
{
virtual void advancedMethod() = 0;
};
struct BaseImpl : public IBase, IExtBase
{
static IBase& initialize(int p_attr)
{
return reinterpret_cast<IBase&>(BaseImpl(p_attr));
}
void basicMethod()
{
m_attr++;
cout << "basic...\tattr: " << m_attr << endl;
}
IExtBase& ext()
{
return reinterpret_cast<IExtBase&>(*this);
}
void advancedMethod()
{
m_attr++;
cout << "advanced...\tattr: " << m_attr << endl;
}
private:
BaseImpl(int p_attr) : m_attr {p_attr} {}
int m_attr;
};
int main()
{
IBase& myBase = BaseImpl::initialize(0);
myBase.basicMethod(); // Only IBase part of the BaseImpl object
myBase.ext().advancedMethod(); // Fails...
return 0;
}
When I try to run this, I get an error:
Unhandled exception at 0x00EFF798 in ProgressiveDisclosure.exe:
0xC0000005: Access violation executing location 0x00EFF798.
I'm guessing it has to do with the casting I'm doing. Furthermore, I find that code rather complex and ugly for such a small example. I fell like there should be another way, by I fail to see it.
Could you please tell me what's wrong here and/or how this could be implemented in a better way?
Thanks.
Aucun commentaire:
Enregistrer un commentaire