Overriding struct variables when subclassing

Discussion in 'C++' started by xamalek@yahoo.com, Sep 8, 2008.

  1. Guest

    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, 2008
    #1
    1. Advertising

  2. On 2008-09-08 18:39, 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
     
    Erik Wikström, Sep 8, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Massimiliano Alberti
    Replies:
    8
    Views:
    4,826
    Massimiliano Alberti
    Feb 7, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,423
    Chris Fogelklou
    Apr 20, 2004
  3. Eric S. Johansson
    Replies:
    3
    Views:
    310
    George Sakkis
    Jun 14, 2006
  4. Frank Millman
    Replies:
    5
    Views:
    296
    Frank Millman
    Sep 8, 2006
  5. Minkoo Seo

    Subclassing Struct.new

    Minkoo Seo, Feb 28, 2006, in forum: Ruby
    Replies:
    15
    Views:
    183
    chiaro scuro
    Mar 4, 2006
Loading...

Share This Page