CObject::AssertValid

Syntax

virtual void AssertValid() const;

Remarks

AssertValid performs a validity check on this object by checking its internal state. In the Debug version of the library, AssertValid may assert and thus terminate the program with a message that lists the line number and filename where the assertion failed.

When you write your own class, you should override the AssertValid function to provide diagnostic services for yourself and other users of your class. The overridden AssertValid usually calls the AssertValid function of its base class before checking data members unique to the derived class.

Because AssertValid is a const function, you are not permitted to change the object state during the test. Your own derived class AssertValid functions should not throw exceptions but rather should assert if they detect invalid object data.

The definition of “validity” depends on the object's class. As a rule, the function should perform a “shallow check.” That is, if an object contains pointers to other objects, it should check to see if the pointers are not null, but should not perform validity testing on the objects referred to by the pointers.

Example

See CObList::CObList for a listing of the CAge class used in all CObject examples.

void CAge::AssertValid() const

{

CObject::AssertValid();

ASSERT( ( m_years > 0 ) && ( m_years < 105 ) );

}