Very basic OOP programming in C
Hi. I have just recentely learned on function pointers.
So now i know how to create a function and assign it a pointer.
So far so good.
But can someone give me a simple example on how I can create a struct and have a function that return a value that is inside that struct ?
And how can I create a constructor function in order to instantiate all the variables inside a struct?
just to explain myself better, let's say I have a struct person:
Code:
struct Person{
int age;
int(*getAge)(int);
};
typedef struct Person Person;
And a function that get's an int and returns it:
Code:
int returnInt(int x){
return x;
}
now i can create a new Person:
Code:
Person p;
p.age = 30;
But how can i link the getAge pointer to the returnInt() function, so that it will be returning the age inside the Person ?
And how can I create some sort of constructor that will automatically initialize all the variables inside a Person struct?
(I can't do something like this:
Code:
struct Person{
int age = 30; // THIS WILL NOT COMPILE
........
};
Thx.