Version 9 (modified by landauf, 16 years ago) (diff) |
---|
SubclassIdentifier
TracNav(TracNav/TOC_Development)?
Description
SubclassIdentifier acts like a pointer to an Identifier with the difference that you can only assign Identifiers of classes inheriting from a given baseclass.
SubclassIdentifier is a template?. The template-argument defines the needed base-class of an assigned Identifier. You can only assign Identifiers representing a class which is derived from the given base-class (or the base-class itself). If you try to assign an Identifier that's not derived from the base-class, you get an error.
Usage
Assignment
You can assign an Identifier either through the constructor or by using the assignment operator=:
// SubClass isA BaseClass: SubclassIdentifier<BaseClass> identifier = Class(SubClass);
Function calls
the operator→ is overloaded an returns the assigned Identifier. That way you can just call functions of the assigned Identifier by using →function():
SubclassIdentifier<BaseClass> identifier = Class(SubClass); identifier->getName(); // returns "SubClass"
fabricate()
There are two possibilities to create an object out of a SubclassIdentifier. Either you just use the fabricate() function of the assigned Identifier through the overloaded operator→. Remember: This function returns a BaseObject* pointer:
// creates a SubClass, returns a BaseObject* pointer identifier->fabricate();
Or you use the function of SubclassIdentifier, this time by using operator., which returns a BaseClass* pointer (BaseClass is the baseclass specified by the template argument):
// creates a SubClass, returns a BaseClass* pointer identifier.fabricate();
Examples
The following examples use the class-tree below.
SubclassIdentifier<A1> myidentifier = Class(A1); // This works SubclassIdentifier<A1> myidentifier = Class(A1B1); // This works SubclassIdentifier<A1> myidentifier = Class(A1B1C1); // This works SubclassIdentifier<A1> myidentifier = Class(BaseObject); // This doesn't work SubclassIdentifier<A1> myidentifier = Class(A3); // This doesn't work SubclassIdentifier<Interface1> myidentifier = Class(A3); // This works SubclassIdentifier<Interface1> myidentifier = Class(A2B2C1); // This works
SubclassIdentifier<A1> myidentifier = Class(A1B1); myidentifier->isExactlyA(Class(A1)); // Returns false myidentifier->isExactlyA(Class(A1B1)); // Returns true (*myidentifier)->getName(); // Returns "A1B1" myidentifier->getName(); // Returns "A1B1" A1* newobject = myidentifier.fabricate(); // Returns a new instance of A1B1, casted to A1