A easy problem about Polymorphic

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Osamede.Zhang

    #1

    A easy problem about Polymorphic

    Compiler store a pointer table in each object with virtual fuction to
    implement polymorphic,isn 't it? But where is the pointer table?
    It seems that object only store a pointer to the table,the output of
    this code is 88.
    #include<iostre am>
    using namespace std;
    class MyClass{
    public:
    MyClass(){ a=0;}
    virtual void tst1() {a-=1;}
    virtual void tst2(){a*=2;}
    virtual void tst3(){a*=1;}
    private:
    int a;
    };
    class Tst:public MyClass{
    void tst1() {std::cout<<"Ts t"<<endl;}
    void tst2(){std::cou t<<"Tst"<<endl; }
    void tst3(){std::cou t<<"Tst"<<endl; }
    };
    int main()
    {
    //Tst tstObject;
    //MyClass *tstp=&tstObjec t;
    //tstp->sub();
    Tst tstObject;
    MyClass myObject;
    cout<<sizeof(my Object);
    cout<<sizeof(ts tObject);
    return 1;
    }

  • Bluse.huang

    #2
    Re: A easy problem about Polymorphic

    when base class has virtual functions, a v_ptr that point to the
    v_table will be add
    so, the object's size is the sizeof(v_ptr) + sizeof(data member)


    "Osamede.Zh ang дµÀ£º
    "
    Compiler store a pointer table in each object with virtual fuction to
    implement polymorphic,isn 't it? But where is the pointer table?
    It seems that object only store a pointer to the table,the output of
    this code is 88.
    #include<iostre am>
    using namespace std;
    class MyClass{
    public:
    MyClass(){ a=0;}
    virtual void tst1() {a-=1;}
    virtual void tst2(){a*=2;}
    virtual void tst3(){a*=1;}
    private:
    int a;
    };
    class Tst:public MyClass{
    void tst1() {std::cout<<"Ts t"<<endl;}
    void tst2(){std::cou t<<"Tst"<<endl; }
    void tst3(){std::cou t<<"Tst"<<endl; }
    };
    int main()
    {
    //Tst tstObject;
    //MyClass *tstp=&tstObjec t;
    //tstp->sub();
    Tst tstObject;
    MyClass myObject;
    cout<<sizeof(my Object);
    cout<<sizeof(ts tObject);
    return 1;
    }

    Comment

    • Osamede.Zhang

      #3
      Re: A easy problem about Polymorphic


      "Bluse.huan g дµÀ£º
      "
      when base class has virtual functions, a v_ptr that point to the
      v_table will be add
      so, the object's size is the sizeof(v_ptr) + sizeof(data member)
      But where is v_table ?heap??textseg?

      Comment

      • Ron Natalie

        #4
        Re: A easy problem about Polymorphic

        Osamede.Zhang wrote:
        Compiler store a pointer table in each object with virtual fuction to
        implement polymorphic,isn 't it? But where is the pointer table?
        It seems that object only store a pointer to the table,the output of
        this code is 88.
        >
        Making a class polymorphic (at least one virtual function) is
        likely to incur overhead. The nature of the overhead is
        implementation specific. While vtables and vptrs are a
        common way to do this, do not be fooled into believing that
        this is defined by the language.

        In the common implementation, each object of a polymorphic
        type will have a pointer to a table. Each defined class
        will create this table somewhere in memory and set it's
        objects pointer to it during construction.

        Comment

        • Ron Natalie

          #5
          Re: A easy problem about Polymorphic

          Bluse.huang wrote:
          when base class has virtual functions, a v_ptr that point to the
          v_table will be add
          so, the object's size is the sizeof(v_ptr) + sizeof(data member)
          >
          >
          There might also be some padding. The object size is not necessarily
          the sum of it's non-static members.

          Comment

          Working...