Thread: Converting C++ code to C

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    1

    Converting C++ code to C

    Hi, below is one of the class in the program. I would like to know how to convert the class into the structure of C.

    Code:
    class node
    {
    public:
        int data;
        node *left,*right;
        int bf;
        node(int d)
        {
    		data=d;
    		left=NULL;
    		right=NULL;
    		bf=0;
        }
        node()
        {
    		data=0;
    		left=NULL;
    		right=NULL;
    		bf=0;
        }
    };
    Thank you.

  2. #2
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    instead of saying class say struct
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    C does not support function overloading, making it difficult to direcly convert your class. It can be done but it would be quite ugly. You could have something similar to this and be quite readable.
    Code:
    struct node {
        int data;
        node *left,*right;
        int bf;
    };
    void node(struct node *curNode, int d) {
        curNode->data=d;
        curNode->left=NULL;
        curNode->right=NULL;
        curNode->bf=0;
    }
    void nodeDefault(struct node *curNode) {
         curNode->data=0;
         curNode->left=NULL;
         curNode->right=NULL;
         curNode->bf=0;
    }
    And to use the two node functions your overloaded in your class, just call the node or nodeDefault functions and pass a pointer to an instance of the structure.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You could also use some #define's to make the differences invisible to the user.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. converting c code to c++, the foreach statement
    By hwttdz in forum C++ Programming
    Replies: 2
    Last Post: 07-12-2006, 11:03 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM