S
huhu1 schrieb:
Also hätte man sich damit kleine Klassen gebastelt?
Wie sieht denn so eine Struktur aus?
so wie this->that geschrieben hat, du musst aber das Objekt immer mitangeben, eine implizite Übergeben gibt es wie bei C++ nicht.
#include <stdio.h>
struct mystr
{
char str[1024];
int (*print)(struct mystr *, size_t);
void (*copy)(struct mystr *, const char*);
};
void copy_str(struct mystr *this, const char *src)
{
strncpy(this->str, src, 1024);
this->str[1023] = 0;
}
int print_str(struct mystr *this, size_t howmany)
{
int x = 0, j;
for(j = 0; j < howmany; ++j)
x += printf("%s", this->str);
return x;
}
int main(void)
{
struct my_str string;
string.print = print_str;
string.copy = copy_str;
string.copy(&string, "Hello World\n");
string.print(&string, 3);
return 0;
}