Solved

How should I encapsulate this?

Posted on 2000-04-06
5
200 Views
Last Modified: 2012-05-04
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
Comment
Question by:klopter
  • 4
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2690190
It sounds like it is a good candidate for polymorphism.  (inheritance)

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2690211
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
 
LVL 22

Expert Comment

by:nietod
ID: 2690225
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
 

Author Comment

by:klopter
ID: 2690504
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
 
LVL 22

Expert Comment

by:nietod
ID: 2690836
>> 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to copy an image file into clipboard C/C++? 1 168
C++ to C# code conversion issue 4 107
c++ syntax question 9 49
trigs fail! I thought I knew how to do trignometry 3 67
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question