共用方式為


CObList::GetHead

Gets the CObject pointer that represents the head element of this list.

CObject*& GetHead( ); 
const CObject*& GetHead( ) const;

Return Value

If the list is accessed through a pointer to a const CObList, then GetHead returns a CObject pointer. This allows the function to be used only on the right side of an assignment statement and thus protects the list from modification.

If the list is accessed directly or through a pointer to a CObList, then GetHead returns a reference to a CObject pointer. This allows the function to be used on either side of an assignment statement and thus allows the list entries to be modified.

Remarks

You must ensure that the list is not empty before calling GetHead. If the list is empty, then the Debug version of the Microsoft Foundation Class Library asserts. Use IsEmpty to verify that the list contains elements.

The following table shows other member functions that are similar to CObList::GetHead.

Class

Member Function

CPtrList

const void*& GetHead( ) const; void*& GetHead( );

CStringList

const CString& GetHead( ) const; CString& GetHead( );

Example

See CObList::CObList for a listing of the CAge class.

The following example illustrates the use of GetHead on the left side of an assignment statement.

const CObList* cplist;

CObList* plist = new CObList;
CAge* page1 = new CAge(21);
CAge* page2 = new CAge(30);
CAge* page3 = new CAge(40);
plist->AddHead(page1);
plist->AddHead(page2);  // List now contains (30, 21). 
// The following statement REPLACES the head element.
plist->GetHead() = page3; // List now contains (40, 21).
ASSERT(*(CAge*) plist->GetHead() == CAge(40));
cplist = plist;  // cplist is a pointer to a const list. 
// cplist->GetHead() = page3; // Error: can't assign a pointer to a const list
ASSERT(*(CAge*) plist->GetHead() == CAge(40)); // OK 

delete page1;
delete page2;
delete page3;
delete plist; // Cleans up memory.      

Requirements

Header: afxcoll.h

See Also

Reference

CObList Class

Hierarchy Chart

CObList::GetTail

CObList::GetTailPosition

CObList::AddHead

CObList::RemoveHead