Generic Methods |
|
We saw that you could create a function as a generic. When it comes to a class, one of the most fundamental ways you can involve a generic is to implement the method of a class as a generic. You can do this by preceding the definition of the method with the declaration of a generic, exactly as we did in the previous sections. Here is an example: public ref class General { public: generic <class T> void Show() { } };
If you plan to process a value of the parameter type in the method, you can pass an argument to the method. Here is an example: public ref class General { public: generic <class T> void Show(T value) { } }; In the body of the method, you can use the argument as you see fit. As we saw in the previous lesson and in the previous sections, at a minimum, you can display the value of the argument by passing it to the Console::WriteLine() method. Before calling the method, you can first declare a variable or a handle of the class. You can then call the method using the period or the arrow operator. Here are examples: using namespace System; public ref class General { public: generic <class T> void Show(T value) { Console::WriteLine(value); } }; int main() { // Call the version of the function that displays an integer int Integer = 246; General gen; Console::Write(L"Value: "); gen.Show<int>(Integer); // Call the version of the function that displays a character __wchar_t Character = L'G'; Console::Write(L"Value: "); gen.Show<__wchar_t>(Character); // Call the version of the function that displays a decimal double DoublePrecision = 355.65; Console::Write(L"Value: "); gen.Show<double>(DoublePrecision); return 0; } This would produce: Value: 246 Value: G Value: 355.65 Press any key to continue . . . If you want to implement the method outside of its class, make sure you precede it with the generic declaration. Here is an example: using namespace System; public ref class General { public: generic <class T> void Show(T value); }; generic <class T> void General::Show(T value) { Console::WriteLine(value); } int main() { return 0; } Remember that you can also declare a class on the managed heap using the gcnew operator and access its member(s) using the -> operator: using namespace System; public ref class General { public: generic <class T> void Show(T value); }; generic <class T> void General::Show(T value) { Console::WriteLine(value); } int main() { // Call the version of the function that displays an integer int Integer = 246; General ^ gen = gcnew General; Console::Write(L"Value: "); gen->Show<int>(Integer); // Call the version of the function that displays a character __wchar_t Character = L'G'; Console::Write(L"Value: "); gen->Show<__wchar_t>(Character); // Call the version of the function that displays a decimal double DoublePrecision = 355.65; Console::Write(L"Value: "); gen->Show<double>(DoublePrecision); return 0; } A generic method can also be declared as static, in which case you would access it using the :: operator. Here is an example: using namespace System; public ref class General { public: generic <class T> static void Show(T value) { Console::WriteLine(value); } }; int main() { // Call the version of the function that displays an integer Console::Write(L"Value: "); General::Show<int>(246); // Call the version of the function that displays a character Console::Write(L"Value: "); General::Show<__wchar_t>(L'G'); // Call the version of the function that displays a decimal Console::Write(L"Value: "); General::Show<double>(355.65); return 0; }
After setting a generic declaration before a method, just as you can pass it as argument, you may want the method to return a value of the parameter type. To do this, simply specify the return type. Here is an example: public ref class General { public: generic <class T> void Show(T value); generic <class T> T GetValue(); }; When implementing the method, make sure you return the parameter type before the method exits. Here is an example: generic <class T> T General::GetValue() { T val; return val; } |
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|