Logo

Managed C++ Keywords: static

 

C++ static Classes

In C++, you must first declare a variable before using it. The C++ language provides an exception to this rule through the static keyword. If you declare a member variable as static in a class, an instance of that class would be made available when the class is used.

If you declare a member variable of a class as static, in C++, you must initialize it globally before using the static member variable. Here is an example:

#include <iostream>
using namespace std;

class CSquare
{
public:
	static double Side;
	CSquare() {};
	void setSide(double S) { Side = S; }
	double getSide() { return Side; }
	double Area() { return Side * Side; }
};

double CSquare::Side = 30.65;

int main()
{
    	// TODO: Please replace the sample code below with your own.
	CSquare Sqr;

	cout << "Square Characteristics";
	cout << "\nSide: " << CSquare::Side;
	cout << "\nArea: " << Sqr.Area() << endl;

	Sqr.setSide(44.28);

	cout << "\nSquare Characteristics";
	cout << "\nSide: " << CSquare::Side;
	cout << "\nArea: " << Sqr.Area();

    	cout << "\n";
	return 0;
}

This would produce:

Square Characteristics
Side: 30.65
Area: 939.422

Square Characteristics
Side: 44.28
Area: 1960.72
Press any key to continue

Notice that, in the above class, we declared the static member variable public, which is sometimes considered bad programming. If you decide to "hide" a member variable in a private section, then you should create a method that would allow the "external world" to access the member variable.

Besides a static method that serves as intermediary between a static member variable and other classes, you can use as many methods as you judge necessary. Here is an example:

#include <iostream>
using namespace std;

class CSquare
{
private:
	static double Side;
public:
	CSquare() {};
	static void setSide(double S) { Side = S; }
	static double getSide() { return Side; }
	static double Area() { return Side * Side; }
};

double CSquare::Side = 30.65;

int main()
{
    	// TODO: Please replace the sample code below with your own.
	CSquare Sqr;

	cout << "Square Characteristics";
	cout << "\nSide: " << CSquare::getSide();
	cout << "\nArea: " << Sqr.Area() << endl;

	Sqr.setSide(44.28);

	cout << "\nSquare Characteristics";
	cout << "\nSide: " << CSquare::getSide();
	cout << "\nArea: " << Sqr.Area();

    	cout << "\n";
	return 0;
}

The result is the same as above

Managed C++ static Classes

The managed C++ language continues with support for the static keyword and embellish it a little bit. We saw that, in C++, a static member variable must be initialized before using it. Managed C++ modifies this.

In managed C++, a static member variable is automatically initialized to 0. Here is an example:

// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

public __gc class CSquare
{
private:
	static double Side;// = 0.00;
public:
	CSquare() {};
	void setSide(double S) { Side = S; }
	double getSide() { return Side; }
	double Area() { return Side * Side; }
};

int _tmain()
{
    	// TODO: Please replace the sample code below with your own.
	CSquare __gc *Sqr = __gc new CSquare;

	Console::WriteLine(S"Square Characteristics");
	Console::WriteLine(S"Side: {0}", __box(Sqr->getSide()));
	Console::WriteLine(S"Area: {0}", __box(Sqr->Area()));

	Sqr->setSide(44.28);

	Console::WriteLine(S"\nSquare Characteristics");
	Console::WriteLine(S"Side: {0}", __box(Sqr->getSide()));
	Console::WriteLine(S"Area: {0}", __box(Sqr->Area()));

    	Console::WriteLine(S"\n");
	return 0;
}

Since a static member variable is automatically initialized to 0, you can change this default value to one you like. In Managed C++, you can initialize a static member variable (only a static member variable) in the class in which in which it is declared. Here is an example:

// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

public __gc class CSquare
{
private:
	static double Side = 40.26;
	
public:
	CSquare() {};
	void setSide(double S) { Side = S; }
	double getSide() { return Side; }
	double Area() { return Side * Side; }
};

int _tmain()
{
	// TODO: Please replace the sample code below with your own.
	CSquare __gc *Sqr = __gc new CSquare;

	Console::WriteLine(S"Square Characteristics");
	Console::WriteLine(S"Side: {0}", __box(Sqr->getSide()));
	Console::WriteLine(S"Area: {0}", __box(Sqr->Area()));

	Sqr->setSide(44.28);

	Console::WriteLine(S"\nSquare Characteristics");
	Console::WriteLine(S"Side: {0}", __box(Sqr->getSide()));
	Console::WriteLine(S"Area: {0}", __box(Sqr->Area()));

    	Console::WriteLine(S"\n");
	return 0;
}
 

As you can see above, unlike C++, in Managed C++, regular member variables give access to static member variables without having to be declared as static. Once again, if you decide to put a static member variable in a private section, you can provide one or more equivalent static methods.

Here is an example:

// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

public __gc class CSquare
{
private:
	static double Side = 40.26;
public:
	CSquare() {};
	static void setSide(double S) { Side = S; }
	static double getSide() { return Side; }
	static double Area() { return Side * Side; }
};

int _tmain()
{
    	// TODO: Please replace the sample code below with your own.
	CSquare __gc *Sqr = __gc new CSquare;

	Console::WriteLine(S"Square Characteristics");
	Console::WriteLine(S"Side: {0}", __box(CSquare::getSide()));
	Console::WriteLine(S"Area: {0}", __box(CSquare::Area()));

	CSquare::setSide(68.18);

	Console::WriteLine(S"\nSquare Characteristics");
	Console::WriteLine(S"Side: {0}", __box(CSquare::getSide()));
	Console::WriteLine(S"Area: {0}", __box(CSquare::Area()));

    	Console::WriteLine(S"");
	return 0;
}

 


Home Copyright © 2004-2010 FunctionX, Inc.