Solved

How should I encapsulate this?

Posted on 2000-04-06
5
198 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now