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