Can someone try to explain me why i get a difference in address of base and derived object while using this pointer and & operator. Below is my code
#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"Base Address "<<this<<endl;
}
};
class B:public A
{
public:
B()
{
cout<<"Deri Address "<<this<<endl;
}
};
int main()
{
B b;
}
The O/P is
Base Address 0x7fff500e9bdf
Deri Address 0x7fff500e9bdf
Both are same.
When i add two more statements in the main as shown below
#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"Base Address "<<this<<endl;
}
};
class B:public A
{
public:
B()
{
cout<<"Deri Address "<<this<<endl;
}
};
int main()
{
B b;
A a;
cout<<&a<<endl<<&b<<endl;
}
The O/P is as shown below
Base Address 0x7fff82c696df
Deri Address 0x7fff82c696df
Base Address 0x7fff82c696de
0x7fff82c696de
0x7fff82c696df
Now I could clearly see the address differs
a) What could be the reason for this difference
b) One more thing i wanted to know is if the base sub object in derived class is exactly same as the base object? I am confused about this because if we say in a base class if we have a variable x and if we have a derived class for this base class(non polymorphic case) now if we talk about the base sub object in derived, is it the same x present in base class is also present in the base sub object of derived class (i mean the variable with same address in both base and base sub object in derived) or we have a separate copy of x in both base and base sub object of derived(i mean the variable with different address)
Please clarify?
You have two objects: a and b:
a with address 0x7fff82c696de
b with address 0x7fff82c696df