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 1 8830
On 2008-09-08 18:39, xa*****@yahoo.c om 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 "license" for more information.
IDLE 0.8 -- press F1 for help
>>> class foo:
dict = {}
string = "foostring"
def bar(self):
|
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 i'd like is a way to get rid of these session variables when we no
longer need them. We originally trie putting cleanup code in Unload but then
realized unload gets called after every page hit, including postbacks, so it
wipes out data the active...
|
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 buffer; /* input buffer */
} MD5_CTX;
....
|
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 reality the setcvar method
is more complicated than this!):
class sup(object):
cvar1 = None
cvar2 = None
|
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;
but compile throw error: error CS0037: ÎÞ·¨½« NULL ת»»³É
| |
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 changing 'self' obviously just alters the
value of that local variable.
Is there some way for me to change the value of the BitSequence
object itself? I've also tried wrapping and delegating using
__getattr__, but I couldn't figure out how to handle...
|
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") = Null
%>
|
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 to be an array.
Example:
Dim ObjParmArr
Dim ArrayCounter
Dim i
Dim ParmList
|
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 nested class 'Inner' with a simple constructor. Outer's
constructor creates an instance of Inner. The code looks like this:
=========
class Outer:
class Inner:
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |