Click here to Skip to main content
15,911,707 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
QuestionClass Reference Variables in Managed C++? Pin
mike montagne28-Sep-06 18:38
mike montagne28-Sep-06 18:38 
AnswerRe: Class Reference Variables in Managed C++? Pin
User 58385228-Sep-06 18:50
User 58385228-Sep-06 18:50 
GeneralRe: Class Reference Variables in Managed C++? Using the Activator Class,,, Pin
mike montagne28-Sep-06 20:12
mike montagne28-Sep-06 20:12 
GeneralRe: Class Reference Variables in Managed C++? Using the Activator Class,,, Pin
User 58385228-Sep-06 20:43
User 58385228-Sep-06 20:43 
GeneralRe: Class Reference Variables in Managed C++? THANKS Pin
mike montagne29-Sep-06 6:09
mike montagne29-Sep-06 6:09 
GeneralRe: Class Reference Variables in Managed C++? Using the Activator Class,,, Pin
mike montagne29-Sep-06 7:33
mike montagne29-Sep-06 7:33 
GeneralRe: Class Reference Variables in Managed C++? Using the Activator Class,,, Pin
led mike29-Sep-06 7:55
led mike29-Sep-06 7:55 
GeneralRe: Class Reference Variables in Managed C++? Using the Activator Class,,, [modified] Pin
mike montagne29-Sep-06 9:29
mike montagne29-Sep-06 9:29 
That's the way it works with Delphi/CPPB class references -- which is desirable for the purposes, and what I'm hoping to emulate/replicate in managed C++.

When you assign the class identifier (name) of a derived class to a class reference and pass the class reference to a factory method, it calls the constructor of the derived class and constructs an instance of the derived class which does not have to be cast. Of course, as the derived class inherits the members of the base class, it is also safe to call the methods of the base class in the derived class instances.

So this capability is exceedingly simple and clean. All you have to do is declare a class reference type, and variables/arguments of this class reference type. The class names/identifiers of the base type and all of its descendants are legal values to assign to the class reference variables or arguments. Your factory method simply takes a class reference and calls the proper constructor.

So for instance (in Delphi syntax), here is one valuable way to implement class references in a factory object:

MyBaseClassReferenceType = class of MyBaseClass; // Type/class definition (complete).

// The following is a field declared in TMyFactoryObject base class, which orients all (similar) methods to any focused FactoryCreationClass descendant:

FFactoryCreationClass: MyBaseClassReferenceType; // Member variable/field. In Delphi, instance types are declared after a colon, which is preceded by the reference (pointer) name.

// "class" methods are roughly the equivalent of static functions of C++. This one "class"/static method instance thus is shared by all TMyFactoryObject instances and descendants, but returns the desired, constructed FactoryCreationClass instance to the calling TMyFactoryObject (as intended):

class function TMyFactoryObject.CreateBaseOrAnyDescendantFactory(TMyFactoryObject: FactoryObjectInstance): MyBaseClassReferenceType; // Return types, in Delphi, are declared after the method signature.
begin
Result := FactoryObjectInstance.FFactoryCreationClass.CommonConstructorName(ArgumentsRequiredByFactoryCreationClass);
end;

This of course is powerful, fast (it's all inherently pointer based), and produces exceedingly light footprints.

So, I understand why you would ask the question, as this does not fit cleanly within the pattern of ANSI C++, because when we call the constructor of the base class name (which has a different identifer/name, versus the shared name of overridden constructors in Delphi) this will only create the parts of the base class. Here, what's happening is the constructor of the intended descendant is indeed called (which inherits the same name, "Create", of the constructor of the base). The nomenclature indicates this descendant constructor (or the base constructor, if the base class is assigned to FFactoryCreationClass). Thus, a descendant instance is returned which does not require casting.

Hmmmm... now that we think of it, probably this ambiguous identification of the necessary constructor explains the need for said Activator class, eh?

Smile | :)


-- modified at 16:01 Friday 29th September, 2006
GeneralRe: Class Reference Variables in Managed C++? Using the Activator Class,,, Pin
Michael Dunn7-Oct-06 7:21
sitebuilderMichael Dunn7-Oct-06 7:21 
GeneralRe: Class Reference Variables in Managed C++? Using the Activator Class,,, Pin
mike montagne10-Oct-06 9:04
mike montagne10-Oct-06 9:04 
GeneralRe: Class Reference Variables in Managed C++? Using the Activator Class... Pin
mike montagne10-Oct-06 9:08
mike montagne10-Oct-06 9:08 
GeneralRe: Class Reference Variables in Managed C++? Using the Activator Class,,, Pin
User 5838522-Oct-06 17:41
User 5838522-Oct-06 17:41 
GeneralRe: Class Reference Variables - Building Template Pin
mike montagne5-Oct-06 9:30
mike montagne5-Oct-06 9:30 
GeneralRe: Class Reference Variables - Building Template Pin
User 5838525-Oct-06 12:48
User 5838525-Oct-06 12:48 
GeneralRe: Class Reference Variables - Building Template Pin
mike montagne5-Oct-06 13:20
mike montagne5-Oct-06 13:20 
QuestionExplicit override of a property and Generic::IEnumeration Pin
User 58385227-Sep-06 20:43
User 58385227-Sep-06 20:43 
AnswerRe: Explicit override of a property and Generic::IEnumeration Pin
George L. Jackson28-Sep-06 16:45
George L. Jackson28-Sep-06 16:45 
GeneralRe: Explicit override of a property and Generic::IEnumeration Pin
User 58385228-Sep-06 18:40
User 58385228-Sep-06 18:40 
QuestionCoGetInterfaceAndReleaseStream() in managed VC++.NET 2003 Pin
lafleon27-Sep-06 8:45
lafleon27-Sep-06 8:45 
QuestionPlease determine the output Pin
Sushant Duggal27-Sep-06 0:58
Sushant Duggal27-Sep-06 0:58 
AnswerRe: Please determine the output Pin
led mike27-Sep-06 8:11
led mike27-Sep-06 8:11 
AnswerRe: Please determine the output Pin
Christian Graus27-Sep-06 12:45
protectorChristian Graus27-Sep-06 12:45 
GeneralRe: Please determine the output Pin
Sushant Duggal27-Sep-06 17:59
Sushant Duggal27-Sep-06 17:59 
GeneralRe: Please determine the output Pin
Christian Graus27-Sep-06 18:06
protectorChristian Graus27-Sep-06 18:06 
GeneralRe: Please determine the output Pin
Sushant Duggal27-Sep-06 18:31
Sushant Duggal27-Sep-06 18:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.