473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Overriding struct variables when subclassing

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

---
Keywords: override, inherit, inheritance, struct, subclass
Sep 8 '08 #1
1 8739
On 2008-09-08 18:39, xa*****@yahoo.com wrote:
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.

--
Erik Wikström
Sep 8 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Eric Baker | last post by:
With this little snippet, i get an inconsistency between the behavior of string and dictionary class variables: Python 2.2.3 (#42, May 30 2003, 18:12:08) on win32 Type "copyright", "credits" or...
2
by: baylor | last post by:
We have numerous pages that store datasets in Session. Sometimes these are used for one page, sometimes for a few (ex. - search page looks up your info, edit person page edits that info) What...
2
by: CViniciusM | last post by:
Hello, a) I have a .h (header file) that declares a struct: .... typedef struct { UINT4 state; /* state (ABCD) */ UINT4 count; /* number of bits, modulo 2^64 (lsb first) */ unsigned char...
12
by: Andrew Jaffe | last post by:
Hi, I have a class with various class-level variables which are used to store global state information for all instances of a class. These are set by a classmethod as in the following (in...
3
by: Ñ©ÔÆÓ¥ | last post by:
Hi,all I have a trouble about struct variable,the detail is : I define a new struct which name ServiceProperty,then I declare a variable like this: ServiceProperty instService = null; ...
4
by: David Coffin | last post by:
I'd like to subclass int to support list access, treating the integer as if it were a list of bits. Assigning bits to particular indices involves changing the value of the integer itself, but...
7
by: Kermit Piper | last post by:
Hello, How can you clear session variables when a reset button is pressed? I thought I might be able to do something like: <% If request.form("Reset") = "Reset" then Session("variable") =...
1
by: andrebarn | last post by:
Hi guys I what to create Dynamic variables when I loop through and recordset, I have read this forum : http://bytes.com/forum/thread436288.html But see my problem is that so af the variables need...
9
by: mrstevegross | last post by:
I ran into a weird behavior with lexical scope in Python. I'm hoping someone on this forum can explain it to me. Here's the situation: I have an Outer class. In the Outer class, I define a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.