-1

I want to draw rectangles in a window in C. I found the following C++ code I want to convert its classes to C struct to use it in my C project. I want to convert the following C++ class to C struct:

class Rect{
public:
    int x1;
    int y1;
    int x2;
    int y2;
    Rect * next;

    Rect(){
        x1 = y1 = x2 = y2 = 0;
        next = NULL;
    }

    void draw(Display* d, Window w, GC gc){
        if((x1<x2) && (y1<y2)){
            XDrawRectangle(d, w, gc, x1, y1, x2-x1, y2-y1);
        }
        if((x1<x2) && (y1>y2)){
            XDrawRectangle(d, w, gc, x1, y2, x2-x1, y1-y2);
        }
        if((x1>x2) && (y1>y2)){
            XDrawRectangle(d, w, gc, x2, y2, x1-x2, y1-y2);
        }
        if((x1>x2) && (y1<y2)){
            XDrawRectangle(d, w, gc, x2, y1, x1-x2, y2-y1);
        }
    }
};

I saw that it's possible to convert some C++ classes to C structs here and here but the above class contains constructor. How can I convert a class which contains constructors into C struct and use it safely?

kenn
  • 328
  • 15
  • 27
  • 1
    Typically you just make a function such as `Rect_create` or `Rect_init` that does the work the constructor would have done. – 0x5453 Mar 03 '21 at 13:57
  • There are a variety of "Object-Oriented C" standards you can apply here. – tadman Mar 03 '21 at 13:59
  • 1
    The first answer in the question you linked explains how to replace the constructor. https://stackoverflow.com/a/41707612/3807729 – Galik Mar 03 '21 at 13:59
  • 1
    This doesn't address the question, but you don't need those parentheses around the `<` and `>` operations. `if (x1y2)` means exactly the same thing as `if ((x1y2))` and doesn't require readers to sort out those extra parentheses. – Pete Becker Mar 03 '21 at 14:04
  • 1
    Close voters: the question is very clear. It s a duplicate though. – Jabberwocky Mar 03 '21 at 14:25
  • I don't understand, why did my question have many downvotes and get closed? – kenn Mar 03 '21 at 14:30
  • Why do you have `next = NULL`? It defaults to `NULL` already – Dock Mar 03 '21 at 14:59
  • @kenn, I didn't downvote your question, but you could have received them because the question reads like "write me a program" (which is out of scope) opposed "here the code I have written so far and my problem is x". To me, you demonstrated effort in pointing out the constructor and by digging up those links. – Allan Wind Mar 03 '21 at 15:06
  • @Dock _"Why do you have next = NULL? It defaults to NULL already"_. No, that's wrong for C and C++. Uninitialized non-global, non-static variables don't have default values: https://wandbox.org/permlink/SmAzCX3cUxFJ3TKx – Thomas Sablik Mar 03 '21 at 16:03

1 Answers1

6

You create a struct with the data and functions for the methods:

struct Rect {
   int x1;
   int y1;
   int x2;
   int y2;
   struct Rect *next;
};

struct Rect *Rect_init(struct Rect *r) {
    assert(r);
    r->x1 = 0;
    r->y1 = 0;
    r->x2 = 0;
    r->y2 = 0;
    r->next = 0;
    return r;
}

void Rect_draw(struct Rect *r, Window w, GC gc) {
    // ...
}

Most c programmers would probably just initialize the value instead of writing that Rect_init() function:

struct Rect r = { 0 };
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • 2
    You could `typedef` the struct to avoid all the unnecessary `struct` keywords: https://wandbox.org/permlink/KsUzNLFWZm4fDldI – Thomas Sablik Mar 03 '21 at 14:21
  • @AllanWind Thank you for your help. You gave me some insight about how to do that. I don't understand why I got so many downvotes. – kenn Mar 03 '21 at 14:28
  • @kenn I didn't get either why you got so many downvotes. Maybe because the answer is actually in the two links you posted. – Jabberwocky Mar 03 '21 at 14:39
  • 1
    @ThomasSablik yes, you could, and it saves 7 key strokes every time you need to the refer to the time. The downside is that it's less explicit so you have to look up the declaration to figure out if a type if a struct, union or enum (minor). typedef 'ed types share namespace with variables which means you cannot no longer use the same name for the type and variable`struct Rect Rect`. – Allan Wind Mar 03 '21 at 14:39
  • @AllanWind I removed my comments regarding the forward declaration, you can remove yours too now. I'll remove this comment in a while. – Jabberwocky Mar 03 '21 at 14:39
  • `struct Rect Rect` really? That should be disallowed anyway. But it seems this is opinion-based. – Thomas Sablik Mar 03 '21 at 15:11