Friday, July 20, 2007

// File: struct-test.cpp
//
// This example shows the use of a structure in C++ and how it behaves much
// like a class including the use of a contructor yet maintains the useability
// of a regular C structure.
#include
#include
using namespace std;
main()
{
struct DataElement {
string SVal;
int iVal;
bool hasData;
DataElement() // Example of a constructor used in a structure.
{
iVal=-1;
hasData=0;
}
} *RealData;
RealData = new DataElement [ 5 ];
// Assignment
RealData[0].SVal = "Value loaded into first structure element.";
RealData[0].hasData = 1; // True

No comments: