I posted earlier (http://stackoverflow.com/questions/1574815/c-oop-in-c-implementation-and-a-bug/1574955#1574955) about my attempt with OOP in C, however as I'm still a new to C, there are a lot of gray areas that are resulting in code issues. I have since tried to implement inheritance, but now I'm getting a new errors, any help here? I've commented in the code below with respect to the warnings I'm getting.
#include <stdio.h>
#include <stdlib.h>
//SPEAKER CLASS
typedef struct speaker {
void (*say)(char *msg);
} speaker;
void say(char *dest) {
//speaker method
printf("%s",dest);
}
speaker* NewSpeaker() {
speaker *s = malloc(sizeof(speaker)); //instantiates a speaker into memory
s->say = say;
return s;
}
//SPEAKER CLASS END
//LECTURER CLASS
typedef struct lecturer {
struct speaker *parent;
void (*say)(struct lecturer *parent,char *msg);
} lecturer;
void lecturer_says(struct lecturer *this,char *msg) {
this->parent->say(msg);
this->parent->say("\nAnd you should be taking notes.\n");
}
lecturer* NewLecturer() {
lecturer *l = malloc(sizeof(lecturer));
l->parent = NewSpeaker();
l->say = lecturer_says;
return l;
}
//LECTURER END
int main() {
speaker *s = NewSpeaker();
s->say("I am a speaker and I can speak.\n");
lecturer *l = NewLecturer();
//compiler gives warning saying i'm giving an imcompatible type, but i'm in fact sending the lecturer obj, why is it an
//incompatible type?
l->say(*l, "I am a lecturer now");
}
Thanks a lot!