Selective Inheritance?

S

Suraj

hello,

I have a base class (Base) with a lot of member variables. The size of
this class is 16bytes.

I need to implement another class (Derived) with partially different
functionality. If I derive Derived from Base, all members of Base
would be available in Derived.

This is unnecessary. In fact, the actual members that are needed just
constitute of 8bytes of data.

I need to develop a mechanism to selectively inherit member variables
and functions from Base into Derived.

Note that any change in Base isn't possible.

1. Is it possible?
2. How?

Thanks
Suraj
 
S

Suraj

It may be unnecessary, but does it cause a problem?



Then you are out of luck.


No.  You can't remove members from a class.  The only option is to
create a new base class with the required subset.

The size does cause a problem. I am talking of a system with a small
amount of user memory.
The no. of objects that can be instantiated can be a big number. Say,
1024 which is considered as a valid and a pretty lightly loaded
configuration.
This is a wastage of 8192 bytes which can be used for other more
important objects of an average size of say 200bytes.

Thanks.
 
F

Fred

The size does cause a problem. I am talking of a system with a small
amount of user memory.
The no. of objects that can be instantiated can be a big number. Say,
1024 which is considered as a valid and a pretty lightly loaded
configuration.
This is a wastage of 8192 bytes which can be used for other more
important objects of an average size of say 200bytes.

Thanks.- Hide quoted text -

- Show quoted text -

If your derived class does not need most of the base calss, then it is
not really a subclass of that base.

Isolate the things that are needed in both classes, and put those
things in a new "NewBase" class.
Then your original base class becomes a subclass of NewBase, and your
original derived class is also a subclass of NewBase.
 
F

Fabrizio J Bonsignore

hello,

I need to develop a mechanism to selectively inherit member variables
and functions from Base into Derived.

class Base {int a;};

class A: public Base {int b;};
class B: public C {int c;};
class D: public D {int d;};
....
class Derived-X: public X; //include all members til class X

I also posed myself this problem when I learned C++ and it IS the C++
problem. Most classes show the same concern as to reduce size for it
and its possible derivations. Some solutions can be quite complex and
are not worth the while when you consider pointers vs architecture
complexity. I think that was the reason for virtual inheritance and
templates to make it into the language... but it only makes sense for
functionality bundles, not to bundle together data types, for this
last case it is much better to make as many structs suitably named as
data type combinations are relevant...

Danilo J Bonsignore
 
Ö

Öö Tiib

One of the prime reasons to make a class is to ensure that an invariant
between two or more variables is never broken, the class becomes the
guardian of the variables so it can restrict operations on them to keep
the invariant. As such, it makes little sense to have a class with only
one variable in it, unless you specifically want to restrict the range
of that variable.

I suspect that you forget number of senses there.

Some that i see to be used in several places:
* encapsulating/wrapping that variable to make it compatible with some
interface.
* hiding information (even type of) that variable.
* restricting transitions between the values of that variable.
* keeping invariant with external single state (variable being id in
some, possibly hidden, storage).
 
G

Goran

hello,

I have a base class (Base) with a lot of member variables. The size of
this class is 16bytes.

I need to implement another class (Derived) with partially different
functionality. If I derive Derived from Base, all members of Base
would be available in Derived.

This is unnecessary. In fact, the actual members that are needed just
constitute of 8bytes of data.

I need to develop a mechanism to selectively inherit member variables
and functions from Base into Derived.

Note that any change in Base isn't possible.

1. Is it possible?

Question is already answered, I just wanted to add "world be worse
off if it was possible". When you derive, derived class IS A base
class. That's the expected behavior, that is the very purpose of
derivation. You derive a class, pass it to any old code that expects
base, and it still works correctly. What you want is to derive, and
then break IS-A relationship. That's just wrong.

Also, given that you want to cut parts of Base, it looks like you know
already what Base does and how (otherwise, you would not know what
members you can leave out). If so, you can just as well duplicate
desired functionality in another, non-related class.

And finally, if you can carve out parts of Base, chances are that Base
already has too much responsibilities, which is a well-known design
smell.

Goran.
 
T

TJorgenson

Question  is already answered, I just wanted to add "world be worse
off if it was possible". When you derive, derived class IS A base
class.

Agreed, with a minor correction...

When you derive _using public inheritance_, derived class IS A base
class.

Tony
 
D

Daniel Pitts

hello,

I have a base class (Base) with a lot of member variables. The size of
this class is 16bytes.

I need to implement another class (Derived) with partially different
functionality. If I derive Derived from Base, all members of Base
would be available in Derived.

This is unnecessary. In fact, the actual members that are needed just
constitute of 8bytes of data.
How about creating a RealBase, which has only the 8 bytes of data, then
making Base derive from RealBase and Derived also from RealBase.

Derived classes should only add functionality and invariants, not remove
them.
I need to develop a mechanism to selectively inherit member variables
and functions from Base into Derived. I doubt you do.

Note that any change in Base isn't possible.
I doubt that's true. It may be undesirable; it may even be impractical.
Neither of those are the same as "not possible". Why don't you think
changing Base is possible? Perhaps someone on here can suggest a way to
do so that doesn't interfere with whatever you think prevents you from
changing Base.
1. Is it possible? Yes.
2. How?
See above.
 
S

Suraj

On 9/20/2010 2:35 PM, Suraj wrote:> hello,




How about creating a RealBase, which has only the 8 bytes of data, then
making Base derive from RealBase and Derived also from RealBase.

Derived classes should only add functionality and invariants, not remove
them.> I need to develop a mechanism to selectively inherit member variables
I doubt you do.


I doubt that's true.  It may be undesirable; it may even be impractical..
  Neither of those are the same as "not possible".  Why don't you think
changing Base is possible? Perhaps someone on here can suggest a way to
do so that doesn't interfere with whatever you think prevents you from
changing Base.


See above.

Well, I agree that it is possible but undesirable due to other
factors. In any case, changing Base isn't an option so, the
implementation will have to live through it.

Perhaps what I was looking for is a "IS A" relation but a "lesser" IS
A rather than "greater" IS A which is what extending a class achieves.

Thanks all
 
Ö

Öö Tiib

The point is that classes with single member variables should be quite
rare.

With that i disagreed. Libraries have often such classes in interface
for hiding information and improving compilation speed.
To have a chain of inheritance from A to X, each with a single
variable is likely silly.

With that i fully agree. Long chains (tall forest) of inheritance add
unneeded complexity, hit performance (by using yo-yo of virtuals) and
make it hard to maintain.
 
J

Jeff Flinn

Suraj said:
Well, I agree that it is possible but undesirable due to other
factors. In any case, changing Base isn't an option so, the
implementation will have to live through it.

Perhaps what I was looking for is a "IS A" relation but a "lesser" IS
A rather than "greater" IS A which is what extending a class achieves.

I think what you have is a "I want it to be" relation.

Jeff
 

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,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top