X
xamalek
I was thinking that when you override a struct variable when you are
subclassing the struct, then the variable in the new struct would
stomp on the old one ... (i.e.) the parent variable (i) and the
subclass variable (i) would share the same pointer ... however I was
wrong, and I want to understand this better.
The question is: When you derive a struct from another struct and you
override some of the struct fields (that were in the parent struct),
why can't you use a generic pointer to the parent struct to access the
common fields?
Can someone explain the principle and the idea behind this and is the
same behavior when using classes to access fields (public member
variables and methods).
See my code below, where it shows that the memory point to by pointers
A* and (B*)(A*) are different, when I thought (erroneously) that they
would be the same.
In the code below I have two variables ...
A *varA;
B varB;
Where B is a child of A.
---------
using namespace std;
#include <iostream>
struct A
{
int *i;
int j;
};
struct B : public A
{
int *i; // Stomp on A.i ???
int j; // Stomp on A.j ???
};
int main()
{
A* varA;
B varB;
int x = 5;
int y = 0;
varB.i = &x;
varB.j = 7;
varA = (A*)(&varB);
cout << "Printing varB.i: " <<
varB.i << endl;
cout << "Printing varA->i Note: varA points to B casted to A*: " <<
varA->i << endl;
cout << "Printing varA->i Note: varA is casted as B*: " <<
((B*)varA)->i << endl << endl;
cout << "Printing varB.j: " <<
varB.j << endl;
cout << "Printing varA->j Note: varA points to B casted to A*: " <<
varA->j << endl;
cout << "Printing varA->j Note: varA is casted as B*: " <<
((B*)varA)->j << endl << endl;
return 0;
}
--------
Running the test as follows ....
[./test] ./a.out
Printing varB.i: 0xbfe4ea9c
Printing varA->i Note: varA points to B casted to A*: 0x50bc338
Printing varA->i Note: varA is casted as B*: 0xbfe4ea9c
Printing varB.j: 7
Printing varA->j Note: varA points to B casted to A*: 1
Printing varA->j Note: varA is casted as B*: 7
subclassing the struct, then the variable in the new struct would
stomp on the old one ... (i.e.) the parent variable (i) and the
subclass variable (i) would share the same pointer ... however I was
wrong, and I want to understand this better.
The question is: When you derive a struct from another struct and you
override some of the struct fields (that were in the parent struct),
why can't you use a generic pointer to the parent struct to access the
common fields?
Can someone explain the principle and the idea behind this and is the
same behavior when using classes to access fields (public member
variables and methods).
See my code below, where it shows that the memory point to by pointers
A* and (B*)(A*) are different, when I thought (erroneously) that they
would be the same.
In the code below I have two variables ...
A *varA;
B varB;
Where B is a child of A.
---------
using namespace std;
#include <iostream>
struct A
{
int *i;
int j;
};
struct B : public A
{
int *i; // Stomp on A.i ???
int j; // Stomp on A.j ???
};
int main()
{
A* varA;
B varB;
int x = 5;
int y = 0;
varB.i = &x;
varB.j = 7;
varA = (A*)(&varB);
cout << "Printing varB.i: " <<
varB.i << endl;
cout << "Printing varA->i Note: varA points to B casted to A*: " <<
varA->i << endl;
cout << "Printing varA->i Note: varA is casted as B*: " <<
((B*)varA)->i << endl << endl;
cout << "Printing varB.j: " <<
varB.j << endl;
cout << "Printing varA->j Note: varA points to B casted to A*: " <<
varA->j << endl;
cout << "Printing varA->j Note: varA is casted as B*: " <<
((B*)varA)->j << endl << endl;
return 0;
}
--------
Running the test as follows ....
[./test] ./a.out
Printing varB.i: 0xbfe4ea9c
Printing varA->i Note: varA points to B casted to A*: 0x50bc338
Printing varA->i Note: varA is casted as B*: 0xbfe4ea9c
Printing varB.j: 7
Printing varA->j Note: varA points to B casted to A*: 1
Printing varA->j Note: varA is casted as B*: 7