Thread: Shifting from C to C++: help with conceptual stuff

  1. #1
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77

    Shifting from C to C++: help with conceptual stuff

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    IMO, you should forget (for the moment) that the languages are superficially similar and just read some good text books on C++.

    Migrating C to C++ (no matter how good the C) is not going to get you a good C++ program. The whole mind-set is completely different.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    Thanks for the thought and link Salem. I have SAMS 'C++ in 21 days...' - but it's not on that list you gave or the recommended books on this website - which makes me wonder if it's any good as an intro text. Well I'll make a start with it anyway and see where it leads

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Because you can't learn C++ in 21 days, or in 21 chapters or 21 anything else.
    You might be able to figure out what any given (small) C++ program does in that time, but expertese needs a lot longer.

    Is it this one?
    http://accu.org/index.php?module=boo...search&rid=699
    Which compiler(s) is it assuming you're going to be using.

    Try to establish how technically up to date it is, and then consider whether the easy read is useful, or just a work of fiction.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Its really hard to take code written procedurally and then automagically change it to object oriented code.

    The strongest point of using C++ is the object oriented programming methodology. You may not have to dump all your code, however you will most likely have to re-write a fair portion of it. The way you think is just different.

    Pick up a good book on object oriented programming and design, along with a good C++ book.

  6. #6
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    Thanks for the replies. I really appreciate these thoughts. I think I was trying to go too fast and your comments have helped me do a rethink and rather than refactor my original code, I'll do a few tutorials and wotnot first.

    Salem - I made a mistake on the book, actually it's this one . I share your thoughts on the '21 days' thing, but there was not a lot of choice in the shop at the time.

    http://www.amazon.com/Sams-Teach-Yourself-One-Hour/dp/0672329417

    the reviews don't look too bad, so hopefully it'll be a decent book to learn from but I'll keep an open mind on it.

    Did you have any books in mind when you suggested a good book on OOP and design cogman?

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    53
    Actually, your code is "almost C++". I see you store function pointers in C structs. You are already coding in an object-oriented fashion, even if you don't realize it yet.

    I think you have quite a headstart on C++ by realizing that one of the major improvements in C++ over C is that this structs-containing-function-pointers are very nicely integrated into the language.

    For most people, it's much easier to learn about something when it is directly applicable to something they've been struggling with before.

    Regards,
    Sander

    --
    Computer Programming: An Introduction for the Scientifically Inclined

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. left shifting signed quantity
    By BEN10 in forum C Programming
    Replies: 6
    Last Post: 04-01-2009, 07:39 AM
  2. Tab key stuff. C+WinAPI is killing me. Please help.
    By Templario in forum Windows Programming
    Replies: 5
    Last Post: 11-21-2002, 03:35 PM
  3. arguments, directories and stuff...
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-26-2002, 05:46 PM
  4. Your stuff
    By smog890 in forum C Programming
    Replies: 6
    Last Post: 06-13-2002, 11:50 PM
  5. Stocks 'n' stuff...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-20-2001, 05:36 PM