Question regarding pointer to virtual member function and pointer todata member

Discussion in 'C++' started by somenath, Aug 29, 2013.

  1. somenath

    somenath Guest

    I have come to know that

    "taking the address of a virtual member function yields its index into its class’s associated virtual table."
    To understand that I wrote the following code.

    #include<iostream>
    using namespace std;

    class Test
    {
    public:
    virtual ~Test() { }
    void f1() {}
    void f2 () {}
    virtual void vf1() {int a; };
    virtual void vf2() {int b; };
    };

    int main (void)
    {
    cout<<"addr of vf1 = "<<&Test::vf1<<endl;
    cout<<"addr of vf2 = "<<&Test::vf2<<endl;
    return 0;
    }

    output
    ++++++

    $ ./a.exe
    addr of vf1 = 1
    addr of vf2 = 1

    I am using cygwin and the g++
    =========
    According to my understanding as the index of function vf1 and vf2 in the virtual table associated with class Test is 1 and 2 the output should also be
    addr of vf1 = 1
    addr of vf2 = 2

    Where I am going wrong ?

    +++++++++++++++++++++++
    I have similar question regarding the pointer to the data member.
    I learned that taking the address of the pointer to the data member yields the "offset within the class object."
    So to understand that I wrote the following program

    #include<iostream>
    using namespace std;
    class Test {
    public:
    void test()
    {
    cout<<"\nInside Test "<<endl;
    }
    float x, y, z;
    int p;
    };

    int main (void)
    {
    float Test::*p = &Test::z;
    cout<<"sizeof float = "<<sizeof (float) <<endl;
    cout<<" val = "<<p;
    return 0;
    }

    +++++++++++
    Output
    $ ./a.exe
    sizeof float = 4
    val = 1
    According my understanding it should be 8.
    Assuming that x starts at 0 and y starts 4 byte apart from x and z starts8 byte apart from x.

    I do not understand why the output is 1.
    Please let me know where I am going wrong?
    somenath, Aug 29, 2013
    #1
    1. Advertising

  2. somenath

    Ike Naar Guest

    Re: Question regarding pointer to virtual member function andpointer to data member

    On 2013-08-29, somenath <> wrote:
    > I have come to know that
    >
    > "taking the address of a virtual member function yields its index into its class?s associated virtual table."
    > To understand that I wrote the following code.
    >
    > #include<iostream>
    > using namespace std;
    >
    > class Test
    > {
    > public:
    > virtual ~Test() { }
    > void f1() {}
    > void f2 () {}
    > virtual void vf1() {int a; };
    > virtual void vf2() {int b; };
    > };
    >
    > int main (void)
    > {
    > cout<<"addr of vf1 = "<<&Test::vf1<<endl;
    > cout<<"addr of vf2 = "<<&Test::vf2<<endl;
    > return 0;
    > }
    >
    > output
    > ++++++
    >
    > $ ./a.exe
    > addr of vf1 = 1
    > addr of vf2 = 1
    >
    > I am using cygwin and the g++
    >=========
    > According to my understanding as the index of function vf1 and vf2 in the virtual table associated with class Test is 1 and 2 the output should also be
    > addr of vf1 = 1
    > addr of vf2 = 2
    >
    > Where I am going wrong ?


    What you see is not what you get. Information is lost during the printing
    of vf1 and vf2; there is no overload for operator<<(void (Test::*)()) so the
    pointer-to-member is silently converted to a bool which is then printed.
    You will see "0" if the pointer is null, and "1" if the pointer is non-null.

    Here's an overload for operator<<() that prints the pointer-to-member as
    an array of unsigned ints; you'll need to #include <cstring> for memcpy().

    std::eek:stream & operator<<(std::eek:stream &os, void (Test::*p)())
    {
    unsigned int buf[sizeof p / sizeof (unsigned int)];
    std::memcpy(buf, &p, sizeof p);
    std::cout << "[" << buf[0];
    for (size_t i = 1; i != sizeof buf / sizeof *buf; ++i)
    {
    std::cout << ":" << buf;
    }
    return os << "]";
    }

    Have fun.
    Ike Naar, Aug 29, 2013
    #2
    1. Advertising

  3. somenath

    somenath Guest

    Re: Question regarding pointer to virtual member function and pointerto data member

    On Thursday, August 29, 2013 11:36:49 AM UTC+5:30, Paavo Helde wrote:
    > somenath
    >
    >
    >
    >
    > > "taking the address of a virtual member function yields its index into

    >
    > > its class’s associated virtual table."

    >
    > [...]
    >
    > > cout<<"addr of vf1 = "<<&Test::vf1<<endl;

    >
    > > cout<<"addr of vf2 = "<<&Test::vf2<<endl;

    >
    > [...]
    >
    > > output

    >
    > > addr of vf1 = 1

    >
    > > addr of vf2 = 1

    >
    > [...]
    >
    > > Where I am going wrong ?

    >
    >
    >
    > By not reading or not understanding or not remembering the responses to
    >
    > your previous question on that same topic, in this very same group. See
    >
    >
    >
    > http://www.cpptalk.net/previous-vt59980.html?postdays=0
    >
    > &postorder=asc&start=0
    >
    >

    I am sorry. I should have done that properly. Thanks
    somenath, Aug 29, 2013
    #3
    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. glen stark
    Replies:
    2
    Views:
    702
    Ron Natalie
    Oct 10, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,043
    Fraser Ross
    Aug 14, 2004
  3. Replies:
    11
    Views:
    690
    James Kanze
    Sep 10, 2006
  4. archimed7592
    Replies:
    4
    Views:
    742
    =?UTF-8?B?UGF3ZcWC?=
    May 30, 2007
  5. Immortal Nephi
    Replies:
    4
    Views:
    777
    Immortal Nephi
    May 4, 2009
Loading...

Share This Page