A easy problem about Polymorphic

Discussion in 'C++' started by Osamede.Zhang, Nov 9, 2006.

  1. 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<iostream>
    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<<"Tst"<<endl;}
    void tst2(){std::cout<<"Tst"<<endl;}
    void tst3(){std::cout<<"Tst"<<endl;}
    };
    int main()
    {
    //Tst tstObject;
    //MyClass *tstp=&tstObject;
    //tstp->sub();
    Tst tstObject;
    MyClass myObject;
    cout<<sizeof(myObject);
    cout<<sizeof(tstObject);
    return 1;
    }
    Osamede.Zhang, Nov 9, 2006
    #1
    1. Advertising

  2. Osamede.Zhang

    Bluse.huang Guest

    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.Zhang дµÀ£º
    "
    > 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<iostream>
    > 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<<"Tst"<<endl;}
    > void tst2(){std::cout<<"Tst"<<endl;}
    > void tst3(){std::cout<<"Tst"<<endl;}
    > };
    > int main()
    > {
    > //Tst tstObject;
    > //MyClass *tstp=&tstObject;
    > //tstp->sub();
    > Tst tstObject;
    > MyClass myObject;
    > cout<<sizeof(myObject);
    > cout<<sizeof(tstObject);
    > return 1;
    > }
    Bluse.huang, Nov 9, 2006
    #2
    1. Advertising

  3. "Bluse.huang дµÀ£º
    "
    > 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?
    Osamede.Zhang, Nov 9, 2006
    #3
  4. Osamede.Zhang

    Ron Natalie Guest

    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.
    Ron Natalie, Nov 9, 2006
    #4
  5. Osamede.Zhang

    Ron Natalie Guest

    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.
    Ron Natalie, Nov 9, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hazzard
    Replies:
    2
    Views:
    636
    Hazzard
    Apr 6, 2004
  2. Manco

    can webmethods be polymorphic?

    Manco, Feb 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    322
    Lionel LASKE
    Feb 3, 2005
  3. pmatos
    Replies:
    3
    Views:
    352
    Victor Bazarov
    Mar 24, 2005
  4. Bruno Desthuilliers
    Replies:
    5
    Views:
    383
    Bruno Desthuilliers
    Aug 29, 2007
  5. Angus

    polymorphic use problem

    Angus, Jul 10, 2008, in forum: C++
    Replies:
    4
    Views:
    297
    Joe Greer
    Jul 10, 2008
Loading...

Share This Page