Re: Idea

From: Val (valmont_programming_at_hotmail.com)
Date: 07/28/04


Date: Wed, 28 Jul 2004 21:23:43 +0200

My goal is to find the best ways to specialize containers wich holds objects (of classes).
And the best ways to make generic containers that implement added functionality of objects of (yet) unknown
classes in general.
Merely exploring.
I'll give you an example of some code. The goal is to make a specialized container for that class. I don't
want to see free functions unless
there are certain overloads (comparison for example) to be implemented. Note that I do not have the skill
whatsoever to implement a specialized class
as you proposed. Yet I want to learn it.

Below is what I consider a crappy design:
//---BEGIN
#include <iostream>
#include <vector>
#include <queue>
#include <functional>
using namespace std;

class SomeClass
{
public:
 int someProperty;
};

//Override original less for any class/struct that holds a "someProperty".
//Assumes operator<() is defined on T.
template<typename T>
class less : binary_function<T, T, bool>
{
public:
 bool operator()(const T& a, const T& b) const
 {
  return a.someProperty < b.someProperty;
 }
};

typedef priority_queue<SomeClass, vector<SomeClass>, less<SomeClass> > PQ;
//For basic function encapsulation demonstrational purpose only!
//Assumes the existence of SomeClass.
//Assumes SomeClass holds a public member "int somePropery".
class PQueue
{
public: //Main methods
 void enterNumber();
 void popPq();
 void showHeap();
 void Menu();
public: //Various
 enum {Exit, Push, Pop, Top};
private: //Members
 PQ MP;
 SomeClass SC;
};

int main()
{
 PQueue TestPQueue;
 TestPQueue.Menu();
 return 0;
}

void PQueue::Menu()
{
 int input=1;
 while (input != 0)
 {
  cout << "\nEnter a Selection:\n";
  cout << "0) Exit\n";
  cout << "1) Push a Number\n";
  cout << "2) Pop the heap\n";
  cout << "3) Show top of heap\n";
  cin >> input;

  switch(input)
  {
  case Exit: cout << "Exited.\n"; break;
  case Push: enterNumber(); break;
  case Pop: popPq(); break;
  case Top: showHeap(); break;
  default: cout << "ERROR, reason: Incorrect Entry\n";
  }
 }
}

void PQueue::enterNumber()
{
 cout << "---> ";
 cin >> SC.someProperty;
 MP.push(SC);
}

void PQueue::popPq()
{
 if(!MP.empty())
 {
  cout << "Popping top of heap (" <<
   MP.top().someProperty << ")" << endl;
  MP.pop();
 }
 else
  cout<<"*Empty Heap*\n";
}

void PQueue::showHeap()
{
 cout << "Top of the heap is: ";
 if(!MP.empty())
  cout<< MP.top().someProperty;
 else
  cout<<"*Empty Heap*";
 cout<<"\n";
}
//---END

What I tried to achieve is to encapsulate additional functions like poPq, enterNumber etc. That includes
showHeap() as well.
As a form of excercise I want to implement it as a part of a speciliazed std::priority_queue. Just because I
want to learn how to do it.



Relevant Pages

  • Re: Generic programming in C
    ... doesn't seem like a good way to kick off a purely technical exchange. ... it seems to work ok for basic containers. ... and with void* you've lost type safety. ...
    (comp.lang.c)
  • Re: Idea
    ... > showHeap() as well. ... your PQueue *in terms of* std::priority_queue. ... bool operator<(const SomeClass& l, const SomeClass& r) ... void enterNumber(); ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Testing if a pointer is valid
    ... data structure, definitely not. ... containers with void* where the type information is passed on to ...
    (comp.lang.c)
  • The proper way of function encapsulation.
    ... //Assumes the existence of SomeClass. ... void enterNumber(); ... PQueue TestPQueue; ...
    (comp.object)
  • Re: The proper way of function encapsulation.
    ... > Perhaps that's what bothers me. ... what is the PQueue class? ... to manipulate a priority queue that contains SomeClass objects. ... void enterNumber() { ...
    (comp.object)