Question regarding pointer to virtual member function and pointer todata member

S

somenath

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?
 
I

Ike Naar

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.
 
S

somenath

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;
[...]

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top