April 6, 2002

Disable Executing More than One Instance of a Program

Sometimes you don’t want to enable the option of running more than one instance of an executable at a time. This can be achieved by using this fragment of code: void main(){ HANDLE hMutex = CreateMutex(NULL, FALSE, “programName”); // if Create mutex succeeded if (hMutex) { // if we already

For Efficiency, Favor Specialized Over Generic Member Algorithms

The algorithms in the standard C++ library are generic and, as such, can be inefficient (or unavailable) with certain containers.For example, the std::swap algorithm is generic but lists define an optimized member function std::list::swap that is much more efficient: std::list list1; std::list list2; // … std::swap(list1, list2); /* Inefficient */

Use C-Style Arrays as Containers

C-style arrays can be passed as containers to STL algorithms. The pointers to array elements can be used as iterators.Example: #include #include int main(){ int array[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 }; const int array_size = sizeof array / sizeof array[0]; std::sort(array, array

Inhibit Derivation from a C++ Class

If no other classes can derive from a class, then you know that all the pointers to that class point to objects of a uniform size. This can simplify memory management and file storage. The idea is to first define a dummy class with a private default constructor. Then virtually

Switch Statements in Object Oriented Design

There is nothing intrinsically evil about ‘switch’ statements. However, they do sometimes result from improper Object Oriented design. There are twodesign principles to keep in mind.1. Open/Closed Principle: this states that each module shall be open for extension but closed for modification.2. Abstract modules should not depend on their details.

Print the Length of the String Without Using strlen()

The following code reads one string, and prints the length of that without using strlen(): #include #include int main(){ char s[20]; gets(s); printf(” %d “,printf(“%s”,s));/*return type of printf() is int. so itreturns the number of characters it prints.*/}

Use a Non-Static Member Variable or Function in a Static Member Function

// This example shows how we can use a non static// member variable or function in a static// member functionclass CClass{public: int m_Var;private: static CClass m_ptClasspublic: static void StaticMemberFonction();}CClass::CClass(){ ptClass = this; // OK}void StaticMemberFonction(){ // m_Var = 10; // WRONG: cannot acces to m_Var // this->m_Var = 10; //WRONG:

Pull a Date From SQL Server DateTime

SQL Server stores datetime data in 8 bytes. The first four bytes represent the date and the second four represent the time. To axe off the time part of the datetime, convert the datetime to a float, truncate the decimals, then convert back to a datetime. Converting directly to an

Add a Tooltip for the MFC Dialog

This method works for the MFC derived class and was tested on a Dialog-based class. Set Active Project Select Project –>Add to Project –>Components and Controls –> VC++ Components –> ToolTip Support Insert! CDialog::OnInitDialog() refer the following: // TODO: Use one of the following forms to add controls: // m_tooltip.AddTool(GetDlgItem(IDC_),

No more posts to show