A
Angus
I have the following classes:
class call {
public:
long GetCallID();
void SetCallID(long callid);
private:
long m_CallID;
};
class tapicall : public call
{
public:
long GetTapiID();
void SetTapiID(long tapiid);
private:
long m_TapiID;
};
class iface
{
public:
call* FindCallByID(long callid);
void CreateNewCall(long callid);
void DeleteCall(long callid);
private:
std::list<call*> m_calllist;
};
Implementation here:
#include "call.h"
long call::GetCallID(){
return m_CallID;
}
void call::SetCallID(long callid){
m_CallID = callid;
}
long tapicall::GetTapiID(){
return m_TapiID;
}
void tapicall::SetTapiID(long tapiid){
m_TapiID = tapiid;
}
call* iface::FindCallByID(long callid){
std::list<call*>::const_iterator it;
for(it = m_calllist.begin(); it != m_calllist.end(); ++it){
if((*it)->GetCallID() == callid)
return *it;
}
return 0;
}
void iface::CreateNewCall(long callid){
call* pCall = new call;
pCall->SetCallID(callid);
m_calllist.push_back(pCall);
}
void iface:eleteCall(long callid){
//do later
}
Then if I do this (in order as below):
void CInherittestDlg::OnOK()
{
m_iface.CreateNewCall(1);
}
void CInherittestDlg::OnBtnSet()
{
tapicall* ptcall = reinterpret_cast<tapicall*>(m_iface.FindCallByID
(1));
if(ptcall) {
ptcall->SetTapiID(3);
}
}
void CInherittestDlg::OnBtnGet()
{
long tapiid;
tapicall* ptcall = reinterpret_cast<tapicall*>(m_iface.FindCallByID
(1));
if(ptcall) {
tapiid=ptcall->GetTapiID();
}
}
Then I get the correct value back.
My understanding problem is that m_iface.FindCallByID returns a
call*. A call* stored in the std::list has only enough memory
reserved to be a call*
And we simply cast to a tapicall* and get the 'extra' bit which is the
TapiID. How does this work at a low level? Surely what is stored in
the iface list is only a call*?
Puzzled as to how it works.
class call {
public:
long GetCallID();
void SetCallID(long callid);
private:
long m_CallID;
};
class tapicall : public call
{
public:
long GetTapiID();
void SetTapiID(long tapiid);
private:
long m_TapiID;
};
class iface
{
public:
call* FindCallByID(long callid);
void CreateNewCall(long callid);
void DeleteCall(long callid);
private:
std::list<call*> m_calllist;
};
Implementation here:
#include "call.h"
long call::GetCallID(){
return m_CallID;
}
void call::SetCallID(long callid){
m_CallID = callid;
}
long tapicall::GetTapiID(){
return m_TapiID;
}
void tapicall::SetTapiID(long tapiid){
m_TapiID = tapiid;
}
call* iface::FindCallByID(long callid){
std::list<call*>::const_iterator it;
for(it = m_calllist.begin(); it != m_calllist.end(); ++it){
if((*it)->GetCallID() == callid)
return *it;
}
return 0;
}
void iface::CreateNewCall(long callid){
call* pCall = new call;
pCall->SetCallID(callid);
m_calllist.push_back(pCall);
}
void iface:eleteCall(long callid){
//do later
}
Then if I do this (in order as below):
void CInherittestDlg::OnOK()
{
m_iface.CreateNewCall(1);
}
void CInherittestDlg::OnBtnSet()
{
tapicall* ptcall = reinterpret_cast<tapicall*>(m_iface.FindCallByID
(1));
if(ptcall) {
ptcall->SetTapiID(3);
}
}
void CInherittestDlg::OnBtnGet()
{
long tapiid;
tapicall* ptcall = reinterpret_cast<tapicall*>(m_iface.FindCallByID
(1));
if(ptcall) {
tapiid=ptcall->GetTapiID();
}
}
Then I get the correct value back.
My understanding problem is that m_iface.FindCallByID returns a
call*. A call* stored in the std::list has only enough memory
reserved to be a call*
And we simply cast to a tapicall* and get the 'extra' bit which is the
TapiID. How does this work at a low level? Surely what is stored in
the iface list is only a call*?
Puzzled as to how it works.