Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: In Mata: Referring to an instance of a class inside an external function?
From
Aljar Meesters <[email protected]>
To
[email protected]
Subject
Re: st: In Mata: Referring to an instance of a class inside an external function?
Date
Fri, 5 Apr 2013 19:17:13 +0200
Dear Yutao,
I needed to adjust your example example a bit (see below) in order to
run it, but than it works as expected (Stata 12.1).
In your example you made a typo (This -> this), maybe this causes the
strange result in your actual situation as well.
Best,
Aljar
//--------------------------------------------------------------------------------------------------
mata
// start class definition
class SomeClass {
pointer scalar P
void new()
void ShowPointer()
void CallExternalFunction() // Added
}
void function SomeClass::new() {
P=&this // This -> 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
// **** "class"
end
//--------------------------------------------------------------------------------------------------
2013/4/5 YutaoSun <[email protected]>
>
> 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/
*
* 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/