blob: 26ff0467767bc5a453ff956d216c78eb4fa94fec (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
// StaticConstructorSample.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
// Sample Files:
#include "MyClass.h"
#include "MyTemplate.h"
// Sample code for StartUp
STATIC_STARTUP_CODE()
{
std::cout << "Starting up..." << std::endl;
}
// Sample code for FinishUp
STATIC_FINISHUP_CODE()
{
std::cout << "Finishing up..." << std::endl;
}
// Template for testing each Type:
template <typename Type>
void TestType(const std::string& strType)
{
using namespace std;
// Static Constructor should have been already invoked:
cout << "Show static data members before creating any instance of " << strType << ":" << endl;
cout << "String: " << Type::StaticStr() << endl;
cout << "PI: " << Type::PI() << endl;
// Temporary block:
// Destructor of local variable will be called at the end of this block.
{
Type myObj;
cout << "Show static data members after creating an instance of " << strType << ":" << endl;
cout << "String: " << myObj.StaticStr() << endl;
cout << "PI: " << myObj.PI() << endl;
}
cout << "Show static data members after destroying the instance of " << strType << ":" << endl;
cout << "String: " << Type::StaticStr() << endl;
cout << "PI: " << Type::PI() << endl;
cout << endl;
}
// Declaration of alias (class names) for the template instaces:
typedef MyTemplate<int> MyTemplateInt;
typedef MyTemplate<double> MyTemplateDouble;
// Invoke the StaticConstructor & StaticDestructor for each one of the template instances:
// Should be called with alias (class names) for the template instaces.
INVOKE_STATIC_CONSTRUCTOR(MyTemplateInt);
INVOKE_STATIC_CONSTRUCTOR(MyTemplateDouble);
// Main:
int _tmain(int argc, _TCHAR* argv[])
{
// Static Constructors have already been called before entering this function...
// Run the same tests for each one of the classes:
TestType<MyClass>("MyClass");
TestType<MyTemplateInt>("MyTemplateInt");
TestType<MyTemplateDouble>("MyTemplateDouble");
// Static Destructors will be called after exiting this function...
return 0;
}
|