Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Declaring class as a function into a class. Why ?

    Dear statalisters,
    I just read this part of the manual.

    Code:
    class U {
    real matrix M
    private real scalar type
    static real scalar count
    class coord scalar c
    real matrix inverse()
    protected real scalar type()
    class coord scalar c()
    }
    I can understand the concept of calling a class into another class, such as that line
    Code:
     class coord scalar c

    Code:
    clear mata
    
    mata:
        class test_into_test {
            string scalar world
            void new()
            }
        void test_into_test::new() {
            world = "world"
            }
    
        class test {
            string scalar hello
            void new()
            class test_into_test scalar c
            }
        void test::new() {
            hello = "hello"
            hello, c.world
            }
    end
    
    mata: a = test()

    But I am particularly interested in the line
    Code:
    class coord scalar c()
    How can a class contain an argument ?

    May you give me more detail about defining a class into another class as a function please ? I thought that a class cannot be defined with any argument...
    Sorry if it has been explained anywhere else. I have not found any information on that.
    Last edited by Charles Cadestin; 02 Dec 2019, 10:51.

  • #2
    Declaring a class-returning function inside another class isn't anything unusual. It's just a member function (class method) that creates an object—perhaps with an option to set a property to some value—and returning the fully formed object. See the example below, where there is a function in class B that creates and returns a scalar object of class A, with a property optionally (my secret) set differently from its default setting. Nothing special.

    Code:
    version 16.0
    
    clear *
    
    mata:
    mata set matastrict on
    
    class A {
        private:
            string scalar my_secret
            void new()
        public:
            final void alterEm()
            final void getMySecret()
    }
    void function A::new() my_secret = "Original!"
    void function A::alterEm(string scalar newun) my_secret = newun
    void function A::getMySecret() printf("%s\n", my_secret)
    
    
    class B {
        public:
            class A scalar getAnA() // <= here
    }
    class A scalar function B::getAnA(| string scalar newun) {
        class A scalar a
    
        if (!args()) return(a)
        else {
            a.alterEm(newun)
            return(a)
        }
    }
    
    void function tester() {
        class A scalar Aobject1, Aobject2
        class B scalar Bobject
    
        Aobject1 = Bobject.getAnA() // Returns an object of class A with default
        Aobject1.getMySecret()
    
        Aobject2 = Bobject.getAnA("Changed!") // Returns an object of class A with option
        Aobject2.getMySecret()
    }
    
    tester()
    
    end
    
    exit

    Comment


    • #3
      Dear Joseph,
      Thank you very much for your reply which perfectly answers the question. That's very helpful!

      Charles

      Comment

      Working...
      X