Static Parameter Class | CodeGuru

Static Parameter Class

Environment: Should run under any OS The problem: The other day I was caught with a dilemma. You see I had a group of several threads that required access to parameters that are currently stored in a file. My first instinct was to just make a class to load the data so that each thread […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 13, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: Should run under any OS

The problem:


The other day I was caught with a dilemma. You see I had a group of


several threads that required access to parameters that are currently


stored in a file. My first instinct was to just make a class to load


the data so that each thread could instantiate a copy of this. This


however just sort of rubbed me the wrong way. There had to be a


better way.


My Solution:


I came up with an idea, which I don

t think is all that new, but it


did catch a few people off guard. I put a little twist on the old


Singleton object. You see I decided to create a Singleton object with


a series of static member functions and member variables. This way I


never have to either instantiate or cleanup the object. And all of my


threads have access to the data without obtaining a referance to it,


because all of the data is availabile through static methods.


My example has all of the thread synchronization removed for clarity,
also I used only the Standard Template Library objects to make
this class portable.

#include <iostream>
using namespace std;
#include “Parameters.h”
void main(int argc, char* argv[])
{
  //**************************************************************
  //* The parameters are accessed without an instance of the class
  //**************************************************************
	cout << "Name: " << CParameters::getName().c_str() << endl;
	cout << "Version " << CParameters::getVersion() << endl;
}

Advertisement

The Header File:


class CParameters
{
public:
	static string getName();
	static int getVersion();
	static void load(string fileName);
	virtual ~CParameters();
private:
	CParameters();
	static string m_ProgramName;
	static int m_Version;
	static CParameters instance;
};

The .cpp File


#include <string>
#include <fstream>
using namespace std;
#include “Parameters.h”
////////////////////////////////////////////////////////////////
// Construction/Destruction
////////////////////////////////////////////////////////////////
//**************************************************************
//* Here I set default values to my static variables
//**************************************************************
string CParameters::m_ProgramName = “Undefined”;
int CParameters::m_Version = 0;
//**************************************************************
//* Here I invoke my constructor and load my variables
//**************************************************************
CParameters CParameters::instance = CParameters();
//**************************************************************
//* Since this is a private method it can only be invoked by the 
//* previous line
//**************************************************************
CParameters::CParameters()
{
	load(“default.dat”);
}
//**************************************************************
//* No cleanup required since we didn’t make anything on the heap
//**************************************************************
CParameters::~CParameters()
{
}
//**************************************************************
//* I put this in to change the values after the program has 
//* started.  Mind you in my case this was unnecessary.
//**************************************************************
void CParameters::load(string fileName)
{
	// Open the parameters file
  ifstream in(fileName.c_str(), ios::in);
	// Did it open?
  if (in)
	{
		// Yes, load up our data
    in >> m_Version >> m_ProgramName;
		// now, close the file
    in.close();
	}
}
//**************************************************************
//* Access methods incase you don’t want to make your static 
//* variable public
//**************************************************************
int CParameters::getVersion()
{
	return m_Version;
}
string CParameters::getName()
{
	return m_ProgramName.c_str();
}

Downloads

Download source – 2 Kb
Download demo project – 5 Kb

History

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.