Tumgik
grgothoughts · 1 year
Text
In my previous post, I talked about a MSVC specific feature where one struct definition could be imported into another struct simply by declaring the imported struct in the other struct.
I discovered this behavior on MSVC because I am working on a way that is user-friendly to code in a more object oriented pattern in C.
I was thinking about class inheritance and how you can access directly to all the super class' members in C++ and I thought that maybe if I was to import a struct into another one I'd get a similar result in C. On Visual Studio it compiles just fine, but the moment you build with a other compiler, it just doesn't work; the compiler doesn't find the members imported from another struct.
I think it's a nice feature to have in places where you want to take a more object oriented approch but want to keep the freedom C gives you.
My approch to OOP in C is to split functionality and state. In the header file, the implementation struct is the struct containing the function pointers the user is expected to use, while the object struct is the states and data the object will hold. Exemple of adder.h:
typedef struct Adder {
int a, b;
} Adder;
const struct IAdder {
void(*ctor)(Adder* this, int a, int b);
int(*result)(Adder* this);
} IAdder;
There should be only one implementation struct per "class" as this is the only instance that should contain the function pointers for class Adder. It should not be modified at anytime during runtime, and so the unique instance is const. Exemple of adder.c:
#include "adder.h"
static void ctor(Adder* this, int a, int b) {
this->a = a;
this->b = b;
}
static int result(Adder* this) {
return this->a + this->b;
}
//We define the unique const instance
//of the implementation struct:
extern const struct IAdder IAdder = {
.ctor = ctor,
.result = result
};
And to use our adder class in main.c:
#include "adder.h"
#include <stdio.h>
int main(int argc, char** argv) {
Adder a = {0};
IAdder.ctor(&a, 3, 5);
int result = IAdder.result(&a);
printf("%i", result);
return 0;
}
9 notes · View notes
grgothoughts · 1 year
Text
Yesterday, I realised there was a feature in MSVC that isn't supported by GGC or LLVM.
Basically, in MSVC, you can make a struct say
struct A {
int attrib_1;
};
In MSVC, you can make a struct that imports the definition of another struct just by declaring in your new struct:
struct B {
struct A;
};
Compiled with MSVC, sizeof(struct B) will be of value "4", but compiled with GCC, it will be of value "0". In other words, MSVC let you import the definition of a struct into another struct, as if you were reusing the definition to extend another struct definition. The exemple above allows you to do
struct B b;
b.attrib_1 = 0;
even if struct B doesn't contain any member named attrib_1 (in this case, it was imported from struct A). This behaviour is not defined by any standards and so this is a MSVC-specific behavior. There are work arround for GCC and other compiler with macros.
2 notes · View notes
grgothoughts · 1 year
Text
I like pigeons. They look friendly.
0 notes
grgothoughts · 1 year
Text
I have failed a uni class last summer and retook the class this winter semester. For the last 10 days, I have been studying and making exercices for that class' final. This morning I did the final exam, and I'm not fully confident that I will pass the exam... not sure what grade to expect.
To have a success and pass the class, you need to have an average of the mid-semester and the final exams greater or equal to 55%, and have a total average of all evaluations greater or equal to 50%.
I'm scared since I failed this class once, I don't want to have to redo it again...
And I'm also tired. Finally finished my winter semester... yet the summer semester starts next week ;-;
0 notes
grgothoughts · 1 year
Text
Hi, this is my second blog where I will be rambling about life.
My main blog is @grgodarkaesthetic.
If you follow my page and your account has no profile picture and no post, I will block you.
0 notes