Best way of sharing a enum

V

Vincent RICHOMME

Hi,

I would like to know what is the best way of doing the following thing.
I am developping a GUI application with a tree and for each item of my
tree I can associate(gui mechanism) a class to store extra information.
So i have this :

ifndef PRJTREE_H
#define PRJTREE_H

#include <wx/treectrl.h>

enum EItemType
{
eRoot = 0,
eReaderGroup,
eReaderItem,
eCardGroup,
eCardItem,
eCardCmdGroup,
eCardCmdItem
};

/*
wxTreeItemData is some (arbitrary) user class associated with some item.
The main advantage of having this class is that wxTreeItemData objects
are destroyed automatically by the tree and, as this class has virtual
destructor
*/
class PrjItemData : public wxTreeItemData
{
public:
PrjItemData(EItemType enuItemType = eRoot) {m_enuItemType =
enuItemType;}

private:
EItemType m_enuItemType;

};



class PrjTree : public wxTreeCtrl
{
public:

PrjTree() { }
PrjTree(wxWindow * parent, wxWindowID id, const wxPoint& pos,
const wxSize& size, long style);
virtual ~PrjTree(){};

void AddItem(EItemType eType, const wxString& strItemName);

DECLARE_DYNAMIC_CLASS(PrjTree)
DECLARE_EVENT_TABLE()
};

And in my AddItem when I add an item to my tree I associate a
PrjItemData with it.

void PrjTree::AddItem(EItemType eParentType, const wxString& strItemName)
{
wxTreeItemId idItem = GetItemCorrespondingTo( eParentType);

AppendItem(idItem, strItemName, -1, -1, new PrjItemData
(eParentType) );
}
#endif



Now my question is should I let enum as global or would it be better to
put it inside one of the two classes?
Should I use namespace ?

I tried to put enum in PrjTree but in this case when I define
PrjItemData I suppose I should write (not even sure if it works) this

class PrjItemData : public wxTreeItemData
{
public:
PrjItemData(PrjTree::EItemType enuItemType = eRoot) {m_enuItemType
= enuItemType;}

private:
EItemType m_enuItemType;

};
 
J

Jonathan Mcdougall

Vincent said:
Hi,

I would like to know what is the best way of doing the following thing.
I am developping a GUI application with a tree and for each item of my
tree I can associate(gui mechanism) a class to store extra information.
So i have this :

ifndef PRJTREE_H
#define PRJTREE_H

#include <wx/treectrl.h>

enum EItemType
{
eRoot = 0,
eReaderGroup,
eReaderItem,
eCardGroup,
eCardItem,
eCardCmdGroup,
eCardCmdItem
};

/*
wxTreeItemData is some (arbitrary) user class associated with some item.
The main advantage of having this class is that wxTreeItemData objects
are destroyed automatically by the tree and, as this class has virtual
destructor
*/
class PrjItemData : public wxTreeItemData
{
public:
PrjItemData(EItemType enuItemType = eRoot) {m_enuItemType =
enuItemType;}

private:
EItemType m_enuItemType;

};



class PrjTree : public wxTreeCtrl
{
public:

PrjTree() { }
PrjTree(wxWindow * parent, wxWindowID id, const wxPoint& pos,
const wxSize& size, long style);
virtual ~PrjTree(){};

void AddItem(EItemType eType, const wxString& strItemName);

DECLARE_DYNAMIC_CLASS(PrjTree)
DECLARE_EVENT_TABLE()
};

And in my AddItem when I add an item to my tree I associate a
PrjItemData with it.

void PrjTree::AddItem(EItemType eParentType, const wxString& strItemName)
{
wxTreeItemId idItem = GetItemCorrespondingTo( eParentType);

AppendItem(idItem, strItemName, -1, -1, new PrjItemData
(eParentType) );
}
#endif



Now my question is should I let enum as global or would it be better to
put it inside one of the two classes?
Should I use namespace ?

The main problem with a global enum is that symbols may clash with
other names. Namespaces could do the job, but then you end up with more
complicated names

namespace EItemType
{
enum Type { eRoot=0 };
}

EItemType::Type t = EItemType::eRoot;

However, if you use 1) more descriptive enum symbols (tree_type_root)
and 2) put the whole thing in a namespace (prjtree for example), a
"global" (now namespace) -scope enum may be alright. That's not what I
recommend though.
I tried to put enum in PrjTree but in this case when I define
PrjItemData I suppose I should write (not even sure if it works) this

class PrjItemData : public wxTreeItemData
{
public:
PrjItemData(PrjTree::EItemType enuItemType = eRoot) {m_enuItemType
= enuItemType;}

If EItemType is on PrjTree, you must qualify the enum symbols:

PrjTree::EItemType e = PrjTree:eRoot;

I would prefer this. You keep type safety and scope.
private:
EItemType m_enuItemType;
};

I once implemented a scoped_enum as a macro which wrapped an enum into
a class, defining some operators and conversions. You may search for it
on google groups, but you'll have to adapt it because it used a non
standard trick (declaration of enums).


Jonathan
 

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
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top