Declaring a static instance of a class like a dynamic one

Well.. Recently I saw a strange way of creating a static instance of a class.
What I mean by “static” here is the “static” in “static vs. dynamic”, not any static variables specified with “static” keyword.

What is the strange of creating a static instance of a class? Here is an example.

MyClass anInstance = MyClass( param1, param2 );

Why is this weird?
First, it looks like hybrid way of creating an instance.
Two examples of creating an instance in static way.

MyClass anInstance;
MyClass anInstance( param1, param2);

Now, this is a dynamic version.

MyClass *pInstance = new MyClass( param1, param2 );

So, the strange looking one seems to call a constructor directly, while it seems to create an instance in static way already. Therefore I thought that dynamic version is somehow created and its content is copied to a static one using a default copy constructor or assign operator.

So, I tested with this codes.

#include <iostream>

using namespace std;

union Unity {

	unsigned long longData;
	unsigned char charArrayData[4];

	Unity( unsigned long unityInLong );
	Unity( unsigned char unitInCharArray[] );
	Unity( Unity *anotherUnity );
};

Unity::Unity(unsigned long unityInLong)
{
	cout << "Unity( unsigned long ) constructor is called" << endl;
	longData = unityInLong;
}

Unity::Unity(unsigned char unitInCharArray[])
{
	cout << "Unity( unsigned char ) constructor is called" << endl;
	memcpy( charArrayData, unitInCharArray, sizeof( charArrayData ) );
}

Unity::Unity( Unity *anotherUnity )
{
	cout << "Unity copy constructor is called" << endl;
	longData = anotherUnity->longData;
}

//////////////////
class UnityClass {

	unsigned long longData;

public:
	UnityClass( void );
	UnityClass( unsigned long unityInLong );
};

UnityClass::UnityClass( void )
{
	cout << "UnityClass( void ) constructor is called" << endl;
	longData = 0;
}

UnityClass::UnityClass( unsigned long unityInLong )
{
	cout << "UnityClass( unsigned long ) constructor is called" << endl;
	longData = unityInLong;
}

int _tmain(int argc, _TCHAR* argv[])
{
	unsigned long longData = 0x01020304;
	unsigned char charArrayData[4] = { 0x01, 0x02, 0x03, 0x04 };

	Unity *pLongUnity = new Unity( longData );

	delete pLongUnity;

	// Is this a correct way of declaring an instance of a union class type?
	// Does it call a copy constructor?
	// 
	Unity staticUnity = Unity( longData );

	// What if general "class" case?

	// Case 1
	UnityClass staticUnityClass = UnityClass( longData ); // This seems to work.

	// Case 2
	UnityClass staticUnityClass2( longData ); // A debugger handles this line the one above, i.e. Case 1

	// Case 3
	UnityClass *pUnityClass = new UnityClass( longData ); // Definitely memory leak

	return 0;
}

Hmm.. however it didn’t call the copy constructor. And when I debugged the code above, it just worked like the Case 2 for “class” case.
( I tested with union and class. )

So, although it looks like dynamic creation with static declaration, no separate dynamic memory was allocated.

When I learnt C++ about 20 years ago, there was no such pattern to create a static version of class instance. No books showed such examples. Even nowadays, I couldn’t find any on-line documents ( even at Stroup’s web site ). So, somebody who didn’t learn the C++ carefully and step-by-step may find the weird pattern accidentally and didn’t recognize that it was not proper way of declaring static instance.

Or.. am I simply ignorant of it?

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: