Else
{ // ,
assert(!"Invalid student data");
Return nullptr;
}
}
//...
test.cpp
#include student.hpp
Int main ()
{
// -.
//
Student * pStudent = Student::Make("Ivanov", 75);
Assert(pStudent);
Delete pStudent;
// -.
//
pStudent = Student::Make(Petrov, 101);
assert(! pStudent);
// -.
//
pStudent = Student::Make(Sidorov, 0);
assert(! pStudent);
}
18. Singleton .
, , . ( , , Windows), ( -, ).
. , , , .
, , (Singleton).
, .
, , . GetInstance, ms_Instance. -, atexit.
printer.hpp
#ifndef _PRINTER_HPP_
#define _PRINTER_HPP_
Class Printer
{ // -, -
static Printer * ms_Instance;
//
Printer ();
//
~Printer ();
// ( )
Printer (const Printer &);
Printer& operator = (const Printer &);
// -
Static void DestroyInstance ();
public:
// -
static Printer * GetInstance ();
//
void Print (const char * _filePath);
};
#endif // _PRINTER_HPP_
printer.cpp
#include printer.hpp
// -, nullptr
Printer * Printer::ms_Instance;
|
|
//
Printer * Printer::GetInstance ()
{ // , - ,
//
if (! ms_Instance)
{ // -
// -
ms_Instance = new Printer();
// main
atexit(& Printer::DestroyInstance);
}
// -
Return ms_Instance;
}
//
void Printer::DestroyInstance ()
{ delete ms_Instance;
ms_Instance = nullptr;
}
//
Printer::Printer ()
{ //
//...
}
Printer::~Printer ()
{ //
//...
}
void Printer::Print (const char * _filePath)
{ // ,
//...
}
test.cpp
#include printer.hpp
Int main ()
{ // -.
Printer* printer = Printer::GetInstance();
//
printer->Print("report.doc");
}
// main Printer::DestroyInstance,
// -