Hello all
I'm trying to make a shift from C to C++. I'm starting by transferring some C code to C++ and in the process also get a better understanding of the underlying conceptual shift. Rather than make bad mistakes early I wondered if I could ask for advice about how I might go about this.
To give an outline of my current C project - it is a (very very) simple GUI written in C and openGL. I have a few objects - a rectangle, label and button. Each object has its own data and includes a struct which I call objectData. The objectData struct is passed over to a "screen manager" which has overall control of the GUI. Here is an outline of the objectData struct:
Code:
typedef struct objectData {
void * address;
enum objectType type;
char name[20];
int tag;
int isSelected; //0 or 1
int functionSelected; //0= none, 1 = move, 2 = resize, 3 = text edit
struct bounds *screenBounds;
} TD_objectData;
A pointer to the objectData struct is added to a databaseRecord when the object is created. The database record looks like this:
Code:
typedef struct objectDatabase {
int numberOfObjects;
struct objectData *objects[MAX_SIZE_DATABASE];
//function pointers
int (*init)(struct objectDatabase *db);
void (*addObject)(struct objectDatabase *db, struct objectData *object);
void (*copyObject)(struct objectDatabase *db, int objectTagOfObjectToBeCopied);
void (*removeObject)(struct objectDatabase *db, int index);
void (*printObject)(struct objectDatabase *db, int index);
void (*printDatabase)(struct objectDatabase *db)
} TD_objectDatabase;
Finally I have made a "screen manager" which is the main interface between user and objects. It can add objects, can identify which objects are clicked on and can change the appearance of the objects which are clicked on (move, resize, colour, edit text).
Here is the interface with the screen manager:
Code:
typedef struct screenManager {
struct objectDatabase database;
struct objectData *selectedObject;
//data which will be used for editing the selected object
float cursorPosOrtho[2];
float colour[4];
//Function pointers
void (*init)(struct screenManager *sm);
//Wrappers around objectDatabase methods
void (*addObject)(struct screenManager *sm, struct objectData *newObject);
void (*copyObject)(struct screenManager *sm, int objectTag);
void (*removeObject)(struct screenManager *sm, int objectTag);
void (*printObject)(struct screenManager *sm, int objectTag);
void (*printDatabase)(struct screenManager *sm);
//New functions
void (*printSelectedObject)(struct screenManager *sm);
void (*createNewObject)(struct screenManager *sm, const char *object);
void (*drawDatabase)(struct screenManager *sm);
int (*whichObject)(struct screenManager *sm, int x, int y);
int (*whichFunction)(struct screenManager *sm, int x, int y);
void (*setCursorPos)(struct screenManager *sm, int x, int y);
void (*callMouseFunctionForSelectedObject)(struct screenManager *sm);
}TD_screenManager;
Apologies in advance for the long chunks of code.
My question is, what shifts do I need to make in my thinking if I am to implement this in C++. Is there anything here which I should abandon, are there any top tips for helping me to restructure the code?
Just as an example, I have started to re-wite the code for the screen manager as a class. Should I make the objectData a class as well? and if so, does this mean that the screen manager should treat the objectData class as a parent class or should it simply include the objectData class as one of its data members????
Thanks in advance with any help on this.