Solved

How should I encapsulate this?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 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