Class and Self Return |
|
When it comes to returning a class after processing it, you have two main alternatives: you can get a class to be returned by an external function or you can create a method that returns its own type of class. To proceed, first declare a method by specifying its return type as the class itself. Because a managed class can only be declared as a handle, the return type must be specified as a handle. Here is an example: public value class CHouse { public: __wchar_t TypeOfHome; int NumberOfBedrooms; double NumberOfBathrooms; Byte Stories; int YearBuilt; double Value; CHouse ^ Create(); }; In the body of the method, proceed as you see fit. As done with other functions, before the end of the method, you must remember to return a handle to the class. Here is an example: CHouse ^ CHouse::Create() { CHouse ^ home = gcnew CHouse; home->TypeOfHome = L's'; home->NumberOfBedrooms = 4; home->NumberOfBathrooms = 3.5; home->Stories = 2; home->YearBuilt = 2002; home->Value = 455960; return home; } After defining a self returning method, you can call it like you would any normal method. Here is an example: using namespace System; public value class CHouse { public: __wchar_t TypeOfHome; int NumberOfBedrooms; double NumberOfBathrooms; Byte Stories; int YearBuilt; double Value; CHouse ^ Create(); void Show(); }; int main() { CHouse ^ House2002 = gcnew CHouse; CHouse ^ Property = House2002->Create(); Property->Show(); Console::WriteLine(); return 0; } CHouse ^ CHouse::Create() { CHouse ^ home = gcnew CHouse; home->TypeOfHome = L's'; home->NumberOfBedrooms = 4; home->NumberOfBathrooms = 3.5; home->Stories = 2; home->YearBuilt = 2002; home->Value = 455960; return home; } C++ proposes an alternative to returning a class from one
of its methods. Instead of explicitly declaring a variable when
implementing a method that returns the same class, the compiler simply needs
to know what object you want to return: the object that called the method or a
newly declared one. If you want to return the same object, you can use a special
pointer called this. When implementing this method, the values of the variables will certainly be modified to implement whatever behavior you want. To return the same object, the this object must be called as a pointer, with *this. This would be done as follows: CHouse ^ CHouse::Create() { return *this; } One way you can use a method that self-returns its class is to initialize its member variables. Since the method returns its class, you can therefore return a pointer to this. Here is an example: using namespace System; public value class CHouse { public: __wchar_t TypeOfHome; int NumberOfBedrooms; double NumberOfBathrooms; Byte Stories; bool HasGarage; int YearBuilt; double Value; CHouse ^ Create(); void Show(); }; int main() { CHouse ^ House2002 = gcnew CHouse; CHouse ^ Property = House2002->Create(); Property->Show(); Console::WriteLine(); return 0; } CHouse ^ CHouse::Create() { TypeOfHome = L'C'; NumberOfBedrooms = 1; NumberOfBathrooms = 1.0; Stories = 1; HasGarage = false; YearBuilt = 1960; Value = 100000; return *this; } void CHouse::Show() { . . . } Instead of returning a handle, since the method that returns the this pointer returns it as a pointer, you can make it return a tracking reference. Here is an example: CHouse % CHouse::Create() { TypeOfHome = L'C'; NumberOfBedrooms = 1; NumberOfBathrooms = 1.0; Stories = 1; HasGarage = false; YearBuilt = 1960; Value = 100000; return *this; } This version of the method would produce the same effect as the previous one.
|
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|