static Collection of objects

A

Audrius

Hello,

what is the best solution to hold the collection of Objects. Lets say I
have such class:

class Task{
private:
CString title;
CString description;
CString owner;
CString date;
CString state;
public:
/* some public methods */

Task();
~Task();
};

This class is used only to hold the data and perform no business logic.
And I have another class, where I want to hold public _static_ variable
of Tasks' collection. It's public and static because I use this class
as mediator in my application. So what collection "manager" should I
use (vector, CObjList, ...)? And can anyone write me working class
"TaskList", where I could have list of Task objects with static
add(Task) and get(int) methods using any suitable collection "manager".

Thank you,
Audrius
 
M

Mike Wahler

Audrius said:
Hello,

what is the best solution to hold the collection of Objects. Lets say I
have such class:

class Task{
private:
CString title;
CString description;
CString owner;
CString date;
CString state;
public:
/* some public methods */

Task();
~Task();
};

This class is used only to hold the data and perform no business logic.
And I have another class, where I want to hold public _static_ variable
of Tasks' collection. It's public and static because I use this class
as mediator in my application. So what collection "manager" should I
use (vector, CObjList, ...)?

There's no 'CObjList' is standard C++, so I'll ignore that. What I
do (and I believe BS recommends) is to 'default' to a vector for
collections, lacking other 'special' features or performance requirements.
IOW vector is imo the simplest, most straightforward, easiest to use and
maintain standard container type. It has a relatively simple interface,
And can anyone write me working class
"TaskList", where I could have list of Task objects with static
add(Task) and get(int) methods using any suitable collection "manager".

// ===========
class TaskList
{
public:
static std::vector<Task> coll;
static void add(const Task& t)
{
/* use vector::push_back or vector::insert, etc /*
}

static int get(const std::vector<Task>::size_type& which)
{
/* would translate to e.g. vector::eek:perator[]() or vector::at() */
/* define translation from your 'Task' type to your return type */
/* of 'int' */
/* etc /*
}
};

std::vector<Task> TaskList::coll;
// ===========

I have no idea what a 'suitable collection manager' is for you, but
the above can be made more generic by using template(s).

Depending upon access/performance needs you could experiment with
other standard container types, e.g. 'std::list', 'std::deque', etc.

-Mike
 
W

wittempj

You can use the stl vector for this, see for a quick example below. I
recommend as well to use stl string for this purpose, as CString is
only known in MFC, it makes your work less portableas this is not
standard C++.

-#include <iostream>
-#include <string>
-#include <vector>
-
-using namespace std;
-
-class Task{
- string m_title;
-public:
- /* some public methods */
- Task(const string& title):m_title(title) {};
- virtual ~Task(){};
-
-};
-
-static vector<Task> tasks;
-
-int main()
-{
- Task t("title");
-
- tasks.push_back(t);
-
- return 0;
-}
 
A

Audrius

Thank you for help. I couldn't make it work when I declared that
collection as variable (attribute) of the class. So i'm using it as
global variable and that solved my problem, though this solution is not
"clean".
 
M

Mike Wahler

Audrius said:
Thank you for help. I couldn't make it work when I declared that
collection as variable (attribute) of the class. So i'm using it as
global variable and that solved my problem, though this solution is not
"clean".

If you post your code and point out what you feel is 'unclean'
(and any other issues that concern you), perhaps we can offer
more specific advice.

I'll take a wild guess that you might believe something is global
when it's not:

Syntax rules dictate that a static class member's definition (other than
that of a constant integral type) must appear outside the class body.
This does *not* make it 'global'. It's still a class member, and subject
to the same scoping and access rules as a non-static member.

-Mike
 
A

Audrius

I admit that, I called static variable that is outside class as global
when in fact it is not. Thanks for clear explanation.

Audrius
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,522
Members
48,229
Latest member
Chelsea806

Latest Threads

Top