• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

How should I encapsulate this?

I have a Class which has two possible types of members.  One type of member needs two lists of
pointers the other needs one lists of pointers.
What is an efficient way to encapsulate this?

Class A {
private:
  bool XYmember;
  int index;
public:
  <list>X listX() ; // Only valid if XYmember == true
  <list>Y listY() ; // Only valid if XYmember == true
  <list>Z listZ() ; // Only valid if XYmember == false
}

These lists will be very short (often empty) and
space is at a premium, so I don't want to
waste the space for empty listX and list Y when XYmember == false.

In C, I might just have arrays for the listX, listY and
listZ pointers and use the index as either an index
into the listX and listY arrays or an index into the
listZ array.

But, I hope that there is a cleaner way to do this in C++.

Note, there is nothing special about the fact that I
am using lists here.  It could be doubles, pointers or any other value that memberXY types need,
but that NOT memberXY types do not need.

Ken
0
klopter
Asked:
klopter
  • 4
1 Solution
 
nietodCommented:
It sounds like it is a good candidate for polymorphism.  (inheritance)

continues
0
 
nietodCommented:
Are you familiar with polymorphism?  Basically in this case you really have two classes, one with two lists and one with one list, but these two classes have some things in common and sometimes might even be used interchangabley.  So what you do is define a base class that has the portions that are in common and then derive classes from this base class that have the "differences"   For example,

class Base
{
   int index;  // the derived classes will both need this index
                 // they also might both need some functions that
                  // deal with the index, those would be defined
                  // in the base class too.
public:
   virtual ~Base(); // Usually a good idea to make the destructor virtual.
};

class Single : public Base
{
public:
    <list>Z listZ() ;
} ;

class Double : public Base
{
public:
   <list>X listX() ;
   <list>Y listY() ;
} ;

continues
0
 
nietodCommented:
If you are not familiar with polymorphism, I would recommend you do a little reading in some C++ texts.  It is a little too big a topic to cover here.  Then if you have questions on it, ask here.  If you are not familar with it, it does take getting used to, but it is _wonderful_.  Very powerful and helps to yield very solid elegant designs.

If you are familiar with it, I can probalby discuss additional details about how you would apply it to your case.
0
 
klopterAuthor Commented:
Your answer makes sense - both suggesting that I use polymorphism here
and that I do a bit of reading first.

I understand polymorphism in concept, but
I haven't used it yet.

What I will probably do here is
start with an inefficient code that
stores all three lists and plan to
add polymorhpism later when I can
no longer waste the space.

Ugly I admit, but I have to weigh the
benefits of using polymorphism today
versus dealying the project.

Ken


 
0
 
nietodCommented:
>> plan to add polymorhpism later
>> when I can no longer waste the space.
I understand you are in a time-bind, but I would seriuously reconsider that.  Adding polymorphism is a lot harder than designing it at first.  It affects the desing at a very basic level.  Specially in cases like this where there is linkely to be a good oportunity to use virtual functions and other wonderful OOP princibles.

At the very least, start your studying now...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now