Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | YutaoSun <yutao.sun.statalist@outlook.com> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | st: In Mata: Referring to an instance of a class inside an external function? |
Date | Fri, 5 Apr 2013 14:38:39 +0200 |
Dear users, I have a tricky question about OOP in Mata. Here is the thing, I have written a class (let's call it "C"), and several EXTERNAL functions. sometimes In the external functions, I need to refer to the instance of "C". I tried This: I put a pointer (let's call it "P") as a member variable of the class and it always points to -&this- and I send the pointer into the external function that needs to refer to the class. When I type "P" in the external function, it gives an address (of course because it's a pointer) and this address is exactly the same as what shows up on the screen when I type "P" in a method of "C". However, when I realize the pointer (i.e. use -(*P)-) inside the external function, it gives "0x0" which I believe is a pointer to "NULL". Here is my code (a piece of pesudo code because the actual situation is a lot complicated than what I explained here). //-------------------------------------------------------------------------------------------------- // start class definition class SomeClass { pointer scalar P void new() void ShowPointer() } void function SomeClass::new() { P=&This } void function SomeClass::ShowPointer() { P eltype(*P) } void function SomeClass::CallExternalFunction() { AnExternalFunction(P) } // external function definition void function AnExternalFunction(pointer scalar Pointer) { Pointer eltype(*Pointer) } // an instance C=SomeClass() C.ShowPointer() // **** this prints out: // **** an address // **** "class" C.CallExternalFunction() // **** this prints out: // **** the same address // **** "pointer" //-------------------------------------------------------------------------------------------------- Does anyone possibly know what's wrong and how to make this happen, or there is a protective machanism going on in Mata? * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/