Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How should I encapsulate this?

Posted on 2000-04-06
5
201 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 Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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.

808 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