Observe the following code:
class A { public: virtual void Foo() = 0; int Bar; };
class B : public A { public: B( float X ); void Foo(); private: float X; };
class C : public A { public: C( float Y ); void Foo(); private: float Y; };
Now, in some function let’s say I do this:
A*& pToA = pToDerived; // allocated as A* pToDerived = new B( ... );
pToA = pToC; // allocated as A* pToC = new C( ... );
Is this valid? If so, will it cause a memory leak even though pToA is a reference to a pointer of pToDerived?
SSCS
Assume that Node has, say, 2 or more types which derive from it. This is a snippet from a linked list I’m implementing at the moment, which will likely require polymorphism in order to work properly, as it’s not meant to be generic. newnode is the argument passed to an insert function.
Node* iNode;
for( iNode = mStart; iNode != mEnd; iNode = iNode->Next )
{
if ( iNode->Key == k ) // Replace current node with newnode
{
newnode->Next = iNode->Next;
newnode->Prev = iNode->Prev;
*iNode = *newnode;
delete newnode;
return; // We're done, so we quit.
}
}
// Node doesn't alreay exist, so we add it.
Node*& uglyhack = mEnd;
iNode->Next = newnode;
newnode->Prev = iNode;
uglyhack = newnode;
Let’s say you have the following:
This code leaks two objects — one of type
B(which happens at thepToA = pToCstatement) and one of typeC(which happens whenmainreturns). The “reference” in this case doesn’t actually enter into much of play here. In C++, references are just aliases for some other object. You cannot “reseat” them. That is, once a reference references something, it can never reference anything else ever again.In this case, when you created pToA, you created a reference to the pointer — the pointer in this case being
pToDerived. Making this reference has no effect on memory managment or responsability of some code to calldeletein the right place whatsoever.