Overriding struct variables when subclassing

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
 
E

Erik Wikström

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

It is very simple, you can not "override" variables in the base class,
you can only declare new ones in the derived class. In other words the
variables in the base will always exist in derived classes, and the
derived classes can contain additional variables. If you want to use the
same variables in both the base class and the derived class you should
declare them protected.
 

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,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top