Introduction to Pointers to Classes
|
|
As done with variables of the regular data types,
you can declare a pointer to a class. Here is an example:
using namespace System;
public value struct CSquare
{
double Side;
};
int main()
{
CSquare *square;
return 0;
}
After
declaring the pointer, you should let the compiler know what
variable the pointer is pointing to. This can easily be done by
assigning the address of an existing object as follows:
using namespace System;
public value struct CSquare
{
double Side;
};
int main()
{
CSquare sqr;
CSquare *square = &sqr;
Console::WriteLine();
return 0;
}
Practical
Learning: Introducing Pointers to Classes |
|
- To declare a pointer to class, change the main() function as follows:
using namespace System;
public value class CProperty
{
public:
__wchar_t TypeOfHome;
int Bedrooms;
float Bathrooms;
Byte Stories;
int YearBuilt;
double Value;
};
int main()
{
CProperty condominium;
CProperty *apartment = &condominium;
Console::WriteLine();
return 0;
}
|
- Save the file
Accessing the Members of a Pointer to a Class
|
|
After declaring a pointer to a class, to use it, you can
access one, a few, or all of its members. You have a choice between two
operators: the period and the arrow.
We saw that the asterisk "*" on the
left side of a pointer represented the value of the variable. In the case of a
class, when writing *sqr, this represents a CSquare object and it holds the
values of the object to which it would have been initialized. To access its members, yon can
use the period operator that is used to
access the values of an object. Because the period has a higher precedence than
the asterisk, you must include the object and its asterisk between
parentheses. Here is an example:
using namespace System;
public value struct CSquare
{
double Side;
};
int main()
{
CSquare sqr;
sqr.Side = 24.48;
CSquare *square = &sqr;
Console::WriteLine("Square Characteristics");
Console::Write("Side: ");
Console::WriteLine((*square).Side);
Console::WriteLine();
return 0;
}
As an alternative, you can access the values using the pointer to the
class. In this case, you would use the name of the pointer and the arrow
operator. Here are examples:
using namespace System;
public value struct CSquare
{
double Side;
};
int main()
{
CSquare sqr;
sqr.Side = 24.48;
CSquare *square = &sqr;
Console::WriteLine("Square Characteristics");
Console::Write("Side: ");
Console::WriteLine(square->Side);
Console::WriteLine();
return 0;
}
Both notations produce the same result:
Square Characteristics
Side: 24.48
Press any key to continue . . .
Practical
Learning: Accessing the Members of a Pointer to a Class |
|
- To access the members of a pointer to class, change the main() function as
follows:
using namespace System;
public value class CProperty
{
public:
__wchar_t TypeOfHome;
int Bedrooms;
float Bathrooms;
Byte Stories;
int YearBuilt;
double Value;
};
int main()
{
CProperty apartment;
CProperty *condominium = &apartment;
condominium->YearBuilt = 1986;
condominium->Bathrooms = 1.5f;
condominium->Stories = 3;
condominium->Value = 348255;
condominium->Bedrooms = 3;
condominium->TypeOfHome = L'T';
Console::WriteLine("=//= Altair Realty =//=");
Console::WriteLine("-=- Properties Inventory -=-");
Console::Write("Type of Home: ");
Console::WriteLine(condominium->TypeOfHome);
Console::Write("Number of Bedrooms: ");
Console::WriteLine(condominium->Bedrooms);
Console::Write("Number of Bathrooms: ");
Console::WriteLine(condominium->Bathrooms);
Console::Write("Number of Stories: ");
Console::WriteLine(condominium->Stories);
Console::Write("Year Built: ");
Console::WriteLine(condominium->YearBuilt);
Console::Write("Monetary Value: ");
Console::WriteLine(condominium->Value);
Console::WriteLine();
return 0;
}
|
|
- Execute the application to see the result
Allocating Memory on the Native Heap
|
|
Using a pointer allows
you to use memory only as needed. This is done by using the asterisk
* and the new operators. The syntax of declaring a pointer to a class is:
ClassName* VariableName = new ClassName;
The ClassName is the class whose variable you want
to declare. It could be in the program or one that shipped with the compiler.
Once again, the asterisk lets the compiler know that the object is declared as a
pointer. The variable name follows the same rules we have applied to other
variables so far.
After declaring the pointer, you can access each of its
members and assign it an
appropriate value. Here are examples:
|
|
using namespace System;
public value struct CSquare
{
double Side;
};
int main()
{
CSquare *square = new CSquare;
square->Side = 24.48;
Console::WriteLine("Square Characteristics");
Console::Write("Side: ");
Console::WriteLine(square->Side);
Console::WriteLine();
return 0;
}
Practical
Learning: Allocating Memory on the Native Heap |
|
- To allocate memory on the heap, change the main() function as follows:
using namespace System;
public value class CProperty
{
public:
__wchar_t TypeOfHome;
int Bedrooms;
float Bathrooms;
Byte Stories;
int YearBuilt;
double Value;
};
int main()
{
CProperty *condominium = new CProperty;
condominium->YearBuilt = 2000;
condominium->Bathrooms = 1.0f;
condominium->Stories = 1;
condominium->Value = 224825;
condominium->Bedrooms = 1;
condominium->TypeOfHome = L'C';
Console::WriteLine("=//= Altair Realty =//=");
Console::WriteLine("-=- Properties Inventory -=-");
Console::Write("Type of Home: ");
Console::WriteLine(condominium->TypeOfHome);
Console::Write("Number of Bedrooms: ");
Console::WriteLine(condominium->Bedrooms);
Console::Write("Number of Bathrooms: ");
Console::WriteLine(condominium->Bathrooms);
Console::Write("Number of Stories: ");
Console::WriteLine(condominium->Stories);
Console::Write("Year Built: ");
Console::WriteLine(condominium->YearBuilt);
Console::Write("Monetary Value: ");
Console::WriteLine(condominium->Value);
Console::WriteLine();
return 0;
}
|
- Execute the application to see the result
- Close the DOS window
De-Allocating Memory from the
Native Heap
|
|
After using a
variable that was declared using the new operator, you can reclaim the
memory space it was using. This is done with the delete operator.
Here is an example:
using namespace System;
public value struct CSquare
{
double Side;
};
int main()
{
CSquare *square = new CSquare;
square->Side = 24.48;
Console::WriteLine("Square Characteristics");
Console::Write("Side: ");
Console::WriteLine(square->Side);
delete square;
Console::WriteLine();
return 0;
}
Practical
Learning: De-Allocating Memory from the
Native Heap |
|
- To de-allocate memory, change the main() function as follows:
using namespace System;
public value class CProperty
{
public:
__wchar_t TypeOfHome;
int Bedrooms;
float Bathrooms;
Byte Stories;
int YearBuilt;
double Value;
};
int main()
{
CProperty *condominium = new CProperty;
. . .
delete condominium;
Console::WriteLine();
return 0;
}
|
- Execute the application to see the result
- Close the DOS window