6 Replies Latest reply on Nov 18, 2009 11:47 PM by harihisu

    OOP : C++ vs AS

    iamfuric

      All

      As most of you know , the C++ programm below otputs string "I'm A", (which i can fully understand):

       

      class A{
      public:
      A(){
        print();
      }
      virtual void print(){
        printf("I'm A\n");
      };
      };
      class B:public A{
      public :
      virtual void print(){
        printf("I'm B\n");
      }
      };
      int _tmain(int argc, _TCHAR* argv[])
      {
      A* bPtr = new B;
      delete bPtr;
      return 0;
      }

      This is not true for the AS alternative : (output is "I'm B")

       

      public class A
      {
        public function A()
        {
         print();
        }
        public function print():void{
         trace("I'm A");
        }

      }
      public class B extends A
      {
        public function B()
        {
         super();
        }
        override public function print():void{
         trace("I'm B");
        }
      }
        var a:A = new B();

       

      How AS knows about B instance while creation of it's parent object instance ?

      Gurus,please clarify

        • 1. Re: OOP : C++ vs AS
          harihisu Level 2

          IMO, that because AS3 has a different mechanism of creating an instance of a class.

           

          In C++, you cannot initialize a variable when declare it inside a class such as:

           

          class A {

          public:

               int i = 1;

               A() {

               }

          }

           

          But you can do that with AS3, and that initialization will be implemented first. You can trace the process by using trace function like this:

           

          public class A {

               var i:int = 1;

               trace( 1 );

               public function A() {

                    trace( 2 );

               }

               trace( 3 );

          }

          // output 1 then 3 then 2

           

          Beside, a function in AS3 is a special kind of variable, instance of class Function. Then, the AVM will initialize the function BEFORE call the constructor. That means function print of class B had the value { trace("I'm B"); } BEFORE we call print() via A's constructor. In this moment, the constructor will refer to the latest print function, then why it print out "I'm B".

           

          Hope it helps.

          1 person found this helpful
          • 2. Re: OOP : C++ vs AS
            iamfuric Level 2

            Thanks for replay

            Digging dipper, it turns out, there is no way to call parent overriden  function explicitly:

             

             

            Looks like  the function is "hard" overriden - no reference to the parent implementation (aka C++ __vfptr).

            AVM  simply instantiatres the functions and then executes them in context of {this} object instance.

            AVM  simply instantiatres the functions and then executes them in context of {this} object instance.

            • 3. Re: OOP : C++ vs AS
              EgorKDie Level 2

              you can get access to the overridden method...

               

              super.print() will call it.

              • 4. Re: OOP : C++ vs AS
                harihisu Level 2

                Yes,

                Just like Java, all method (function) of a class is "virtual". And like EgorKDie said, "super" is superb in this case

                • 5. Re: OOP : C++ vs AS
                  iamfuric Level 2

                  I meant "from outside".

                  In C++ you can :

                   

                  B b;

                  b.print(); // I'm B

                  ((A)b).print(); // I'm A

                   

                  but in AS:

                   

                  var a:A = new B();

                  a.print(); // I'm B

                  (a as A).print(); // I'm still  B

                   

                   

                   

                  • 6. Re: OOP : C++ vs AS
                    harihisu Level 2

                    As I said before, Actionscript 3 is Java-like.

                     

                    Refer to the first answer in this link:

                    http://stackoverflow.com/questions/1032847/can-java-call-parent-overridden-method-in-other -objects-but-not-subtype

                    , call superclass method outside the subclass is prohibited due to encapsulation violation.  IMO, C++ is not a pure OOP (thus it doesn't fully support OOP characteristics), that's why you can easily call parent class' method via casting.