Home

Classes and Constants

 

Static Constants

In C++, you are not allowed to initialize a variable in the body of a class. Consider the following:

#pragma once

namespace Geometry
{
    public ref class CCircle
    {
	public:
        double Radius;
        double Area();

	const double Pi = 3.14159;
    };
}

This class will not compile and you would receive the following error:

error C3845: 'Geometry::CCircle::Pi': 
	only static data members can be initialized 
	inside a ref class or value type

As an alternative, you can declare the variable as a static constant. When creating the constant, you can type the static keyword before or after the data type. Here is an example:

#pragma once

namespace Geometry
{
    public ref class CCircle
    {
	public:
        double Radius;
        double Area();

	static const double Pi = 3.14159;
    };
}

After creating the constant, you can use it as you see fit. To access the constant, you can use it by its name:

#include "circle.h"

namespace Geometry
{
    double CCircle::Area()
    {
	return this->Radius * this->Radius * Pi;
    }
}

You can also access it as you would a static member of a class, using the "::" operator. Here is an example:
Header File: Circle.h
#pragma once

namespace Geometry
{
    public ref class CCircle
    {
	public:
        double Radius;
        double Area();

	static const double Pi = 3.14159;
    };
}
Source File: Circle.cpp
#include "circle.h"

namespace Geometry
{
    double CCircle::Area()
    {
	return this->Radius * this->Radius * CCircle::Pi;
    }
}
Sourcce File: Exercise.cpp
#include "Circle.h"

using namespace System;
using namespace Geometry;

CCircle ^ CreateCircle()
{
    Console::Write(L"Enter the radius of the circle: ");
    CCircle ^ circle = gcnew CCircle;
    circle->Radius = double::Parse(Console::ReadLine());

    return circle;
}

void DescribeCircle(CCircle ^ %round)
{
    Console::WriteLine(L"Circle Description");
    Console::WriteLine(L"Radius: {0:F}", round->Radius);
    Console::WriteLine(L"Area:   {0:F}", round->Area());
}

int main()
{
    CCircle ^ figure = CreateCircle();
    Console::WriteLine();
    DescribeCircle(figure);

    Console::WriteLine();

    return 0;
}

Here is an example of running the program:

Enter the radius of the circle: 48.12

Circle Description
Radius: 48.12
Area:   7274.46

Press any key to continue . . .

Literal Constants

C++/CLI provides an additional way to create a constant in the body of a class. This is done using the literal keyword. Like the const keyword, the literal keyword is followed by the data type, a name for the constant, the assignment operator, the assigned value, and the semi-colon. Here is an example:

#pragma once

namespace Geometry
{
    public ref class CCircle
    {
	public:
        double Radius;
        double Area();

	literal double Pi = 3.14159;
    };
}

After creating the literal constant, you can access it using its name. Here is an example:

#include "circle.h"

namespace Geometry
{
    double CCircle::Area()
    {
	return this->Radius * this->Radius * Pi;
    }
}

You can also qualify its name using the :: operator. This would be done as follows:

#include "circle.h"

namespace Geometry
{
    double CCircle::Area()
    {
	return this->Radius * this->Radius * CCircle::Pi;
    }
}

Like the other member variables, you can create a literal constant in either the public and private sections, and it would follow the rules of that section.

 

Previous Copyright © 2006 FunctionX, Inc. Next