cannot access virtual functions in an instance retrieved using CONTAINING_RECORD
I am using windows programming with C++ and have below code:
class A
{
public:
virtual void Func1 () {}
void Func2 () {}
}
class B : public A
{
public:
__override virtual void Func1 () {}
}
I made a double linked list of B objects using LIST_ENTRY and tried to visit an element in this list in below way:
LIST_ENTRY * pEntry; // I got this pointer using RemoveHeadList
A * pA;
pA = CONTAINING_RECORD (pEntry, A, m_le);
pA->Func2 (); // works fine
pA->开发者_如何学C;Func1 (); // Access violation
As you see, the pointer retrieved using CONTAINING_RECORD cannot call a virtual function. What could be wrong? Thanks for any help.
From what I can see in the code, CONTAINING_RECORD
is returning an invalid pointer. Show us the code for it. Func2 wprks just by chance, because is not dependant on the this pointer, but is nevertheless ilegal.
精彩评论