Inheritance of objects within objects

S

Simon Elliott

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!
 
R

Rolf Magnus

Simon Elliott said:
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.
 
S

Simon Elliott

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.
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top