Inheritance of objects within objects

Discussion in 'C++' started by Simon Elliott, Dec 10, 2004.

  1. I want to do something along these lines, but with one crucial
    difference:

    class foo1
    {
    public:
    foo1(void){}
    void Test1(void)
    {
    i1 = 1;
    }
    protected:
    struct b1
    {
    int i1;
    } bar;
    };

    class foo2:public foo1
    {
    public:
    foo2(void){}
    void Test2(void)
    {
    i1 = 1;
    i2 = 2;
    }
    protected:
    struct b2:public b1
    {
    int i2;
    } bar;
    };

    In the above, there are two instances of bar - foo1::bar and foo2::bar.
    I want to do something similar, but with only one instance of bar.

    In other words I want foo2 to inherit from and extend the functionality
    of foo1, and I want foo2 to contain an extended bar.

    So foo1::Test1 would be accessing foo1::bar.i1 and so would foo2::Test2.

    I'm fairly sure that this can't be done, but perhaps some of the more
    experienced developers here know something I don't!


    --
    Simon Elliott http://www.ctsn.co.uk
     
    Simon Elliott, Dec 10, 2004
    #1
    1. Advertising

  2. Simon Elliott

    Rolf Magnus Guest

    "Simon Elliott" <Simon at ctsn.co.uk> wrote:

    > I want to do something along these lines, but with one crucial
    > difference:
    >
    > class foo1
    > {
    > public:
    > foo1(void){}
    > void Test1(void)
    > {
    > i1 = 1;
    > }
    > protected:
    > struct b1
    > {
    > int i1;
    > } bar;
    > };
    >
    > class foo2:public foo1
    > {
    > public:
    > foo2(void){}
    > void Test2(void)
    > {
    > i1 = 1;
    > i2 = 2;
    > }
    > protected:
    > struct b2:public b1
    > {
    > int i2;
    > } bar;
    > };
    >
    > In the above, there are two instances of bar - foo1::bar and foo2::bar.
    > I want to do something similar, but with only one instance of bar.
    >
    > In other words I want foo2 to inherit from and extend the functionality
    > of foo1, and I want foo2 to contain an extended bar.
    >
    > So foo1::Test1 would be accessing foo1::bar.i1 and so would foo2::Test2.
    >
    > I'm fairly sure that this can't be done, but perhaps some of the more
    > experienced developers here know something I don't!


    I don't think either that this can be done. What you could do is make foo1
    into a foo_base class from which foo1 and foo2 are derived. Then foo1 can
    contain an instance of the foo_base version of your nestest struct, and
    foo2 can derive from it and contain an instance of that derived struct.
     
    Rolf Magnus, Dec 10, 2004
    #2
    1. Advertising

  3. On 10/12/2004, Rolf Magnus wrote:
    > I don't think either that this can be done. What you could do is make
    > foo1 into a foo_base class from which foo1 and foo2 are derived. Then
    > foo1 can contain an instance of the foo_base version of your nestest
    > struct, and foo2 can derive from it and contain an instance of that
    > derived struct.


    I'm always amazed at the fast response I get from this group. Thanks
    for the suggestion: it's probably as close as I can get to my original
    requirement.

    --
    Simon Elliott http://www.ctsn.co.uk
     
    Simon Elliott, Dec 10, 2004
    #3
    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. maxw_cc
    Replies:
    1
    Views:
    3,173
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    840
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,060
  4. Brandon Coleman

    Objects within objects... ??

    Brandon Coleman, Jul 19, 2006, in forum: Ruby
    Replies:
    6
    Views:
    122
    Wes Oldenbeuving
    Jul 20, 2006
  5. Replies:
    1
    Views:
    92
    Matt Kruse
    Aug 15, 2006
Loading...

Share This Page