Bookmark and Share

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]

st: Question about Mata classes and Static keyword


From   Erik Lindsley <[email protected]>
To   [email protected]
Subject   st: Question about Mata classes and Static keyword
Date   Wed, 01 Feb 2012 09:39:59 -0600

Greetings,
  I hope I am posting to the right list about my Stata/Mata programming question. If there is a another list I should be using please just let me know.

 I am trying to use the static keyword with a Mata class object, but my _static_ variables seem to disappear when accessed from inside a _static_ function.

It seems that from within _static_ functions, NO outside variables can be accessed, including other _static_ class variables?!?
Perhaps I am not understanding the semantics of the static keyword, as this behavior seems to be a departure from the "static" keyword found in other programming languages (such as C/C++)?

For the record, I am running the latest binary (01 Sep 2011, according to update query) of StataSE 11.2.

Any help would be greatly appreciated...
Sincerely,
 - Erik Lindsley


P.S.: Here is a simple test case to illustrate my problem, along with the output....

mata:
 class STest {
 	private static real scalar s 
 	public:
 	void setLocal()
 	real scalar getLocal()
 	static void setStatic()
 	static real scalar getStatic()
 	
 }	
 	
 	void STest::setLocal(real scalar val) {
 		s=val
 	}
 	
 	real scalar STest::getLocal() {
 		return(s)
 	}


 	void STest::setStatic(real scalar val) {
 		s=val
 	}
 	
 	real scalar STest::getStatic() {
 		return(s)
 	}



void runTest() {
	class STest scalar ST
	ST.setLocal(-1)
	printf("STest::getLocal returned %f  (-1 was expected)\n",ST.getLocal());
	printf("STest::getStatic returned %f  (-1 was expected)\n",STest::getStatic());
	
}

runTest()
/*
Output (fails at run time)....
: runTest()
STest::getLocal returned -1  (-1 was expected)
      STest::getStatic():  3204  s[0,0] found where scalar required [2]
               runTest():     -  function returned error [5]
                 <istmt>:     -  function returned error [1]
*/



end


 
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index