Home

Function Templates and Pointers

 

A Template Parameter as a Reference

As you can use references for a regular variable, you can create a template function parameter as a reference. To do this, in the parentheses of the function, precede the name of the parameter with the & operator. Here is an example:

template <class TypeOfValue>
void Show(TypeOfValue &value)
{
    Console::WriteLine(value);
}

When calling the function, make sure you pass the argument as a reference. Here are examples:

using namespace System;

template <class TypeOfValue>
void Show(TypeOfValue &value)
{
    Console::WriteLine(value);
}

int main()
{
    // Call the function and pass it an integer value
    int a = 246;
    int &b = a;
    Show<int>(b);

    // Call the function and pass it a character value
    __wchar_t u = L'G';
    __wchar_t &v = u;
    Show<__wchar_t>(v);

    // Call the function and pass it a double-precision value
    double x = 355.65;
    double &y = x;
    Show<double>(y);

    return 0;
}

A Template Parameter as a Pointer

Instead of declaring a template parameter as a regular type, you can create it as a pointer. To do this, precede the Argument factor with an asterisk. Remember that, when using the parameter, you must process it as a pointer. Here is an example:

template <class TypeOfValue>
void Show(TypeOfValue *value)
{
    Console::WriteLine(*value);
}

When calling the function, apply the rules of pointers passed to functions. Here are examples:

using namespace System;

template <class TypeOfValue>
void Show(TypeOfValue *value)
{
    Console::WriteLine(*value);
}

int main()
{
    // Call the function and pass it an integer value
    int a = 246;
    Show<int>(&a);

    // Call the function and pass it a character value
    __wchar_t u = L'G';
    Show<__wchar_t>(&u);

    // Call the function and pass it a double-precision value
    double x = 355.65;
    Show<double>(&x);

    return 0;
}

Remember that you can first declare a pointer variable, initialize it, and then pass it to the function. Here are examples:

using namespace System;

template <class TypeOfValue>
void Show(TypeOfValue *value)
{
    Console::WriteLine(*value);
}

int main()
{
    // Call the function and pass it an integer value
    int *a = new int(246);
    Show<int>(a);

    // Call the function and pass it a character value
    __wchar_t *u = new __wchar_t(L'G');
    Show<__wchar_t>(u);

    // Call the function and pass it a double-precision value
    double *x = new double(355.65);
    Show<double>(x);

    return 0;
}

A Template Parameter as a Handle

If you declare a variable as a regular value, it would be stored in the stack. If you declare it as a pointer, it would be stored in the native heap. If you want to process a variable in the managed heap, create it as handle, applying the rules we have learned in previous lessons. For a template, pass the parameter as a handle. If you want to display it on the console, pass its pointer to the Console::WriteLine() method. This can be done as follows:

using namespace System;

template <class TypeOfValue>
void Show(TypeOfValue ^ value)
{
    Console::WriteLine(value);
}

Before calling the method, make sure you have created a variable as a handle and pass it to the function. This would be done as follows:

int main()
{
    // Call the function to display the number of pages of a book
    int ^ a = gcnew int(704);
    Console::Write(L"Number of Pages: ");
    Show<int>(a);

    // Call the function to display the character gender
    __wchar_t ^ u = gcnew __wchar_t(L'M');
    Console::Write(L"Employee Gender: ");
    Show<__wchar_t>(u);

    // Call the function to display somebody's hourly salary
    double ^ x = gcnew double(18.48);
    Console::Write(L"Hourly Salary:   ");
    Show<double>(x);

    return 0;
}

As you should know already, you can pass a handle to String to the Console::WriteLine() method to display on the console. In the same way, you can pass a String value to a template function. Here is an example:

using namespace System;

template <class TypeOfValue>
void Show(TypeOfValue ^ value)
{
    Console::Write(value);
}

int main()
{
    // Call the function to display the number of pages of a book
    String ^ strMessage = L"Number of Pages: ";
    int ^ a = gcnew int(704);
    Show(strMessage);
    Show(a);
    Console::WriteLine();

    // Call the function to display the character gender
    strMessage = L"Employee Gender: ";
    __wchar_t ^ u = gcnew __wchar_t(L'M');
    Show(strMessage);
    Show(u);
    Console::WriteLine();

    // Call the function to display somebody's hourly salary
    strMessage = L"Hourly Salary:   ";
    double ^ x = gcnew double(18.48);
    Show(strMessage);
    Show<double>(x);
    Console::WriteLine();

    return 0;
}

This would produce:

Number of Pages: 704
Employee Gender: M
Hourly Salary:   18.48
Press any key to continue . . .

A Function Template With Various Parameters

Just like a function can take one argument, it can take various template parameters. You can pass one argument as a known type and the other as a template parameter. Here is an example:

using namespace System;

template <class TypeOfValue>
void Show(String ^ msg, TypeOfValue value)
{
    Console::WriteLine(L"{0}: {1}", msg, value);
}

int main()
{
    // Call the function and pass it an integer value
    Show(L"Integer", 246);
    // Call the function and pass it a character value
    Show(L"Character", 'G');
    // Call the function and pass it a double-precision value
    Show(L"Decimal", 355.65);

    return 0;
}

This would produce:

Integer: 246
Character: 71
Decimal: 355.65
Press any key to continue . . .

In the same way, you can pass different parameters to the function, and you can pass different known types and different parameter types, in any order of your choice.

Although we directly passed the values to the function when calling it, you can first declare a variable before passing it to the function. Here are examples:

using namespace System;

template <class TypeOfValue>
void Show(String ^ msg, TypeOfValue value)
{
    Console::WriteLine(L"{0}: {1}", msg, value);
}

int main()
{
    // Call the function and pass it an integer value
    const int iValue = 246;
    Show(L"Integer", iValue);
    // Call the function and pass it a character value
    const __wchar_t cValue = L'G';
    Show(L"Character", cValue);
    // Call the function and pass it a double-precision value
    const double dValue = 355.65;
    Show(L"Decimal", dValue);

    return 0;
}

The other concepts we have reviewed so far are still valid. For example, you can pass the parameter by value, by reference, as a pointer, as a constant reference, or as a constant pointer. Here is the parameter passed as a constant reference:

template <class TypeOfValue>
void Show(String ^ msg, const TypeOfValue &value)
{
    Console::WriteLine(L"{0}: {1}", msg, value);
}

You can also involve the argument in conditional statements as you see fit.

 

Previous Copyright © 2006-2016, FunctionX, Inc. Next