how to interpret typedefs

File hello.h

/* hello.h */

#include <cstdio>

class A {
public:
A();
void init1();
inline void fun () { printf ("A::fun (this=%p)\n", this); }
};

extern "C" A* createA();




File
/* hello.cc */
#include <cstdio>
#include "hello.h"

A::A() {
init1();
}

void A::init1() {
printf("A::init1 this=%p\n", this);
}

extern "C" A* createA() {
return new A;
}


File main.cpp

#include <cstdio>
#include <iostream>
#include <dlfcn.h>
#include "hello.h"

using namespace std;
typedef A* fun_createA();

int main() {
void *handle;
fun_createA *ptr_createA;
A *myA;

handle = dlopen("./libhello.so", RTLD_LAZY | RTLD_GLOBAL);
if (!handle) {
cout << "The error is " << dlerror();
}
ptr_createA = (fun_createA *)dlsym (handle, "createA");
printf ("\n ptr_createA is %p\n", ptr_createA);
myA = ptr_createA();
printf ("ptr_createA gave me %p\n", myA);
myA->fun ();
return 0;
}


The build part is


g++ -g -fPIC -c hello.cc
g++ -g -shared -o libhello.so hello.o
g++ -g -o myprog main.cpp -ldl



I am new to typedef what is the meaning for
following code lines


1) typedef A* fun_createA();

2) fun_createA *ptr_createA;
3) A *myA;
...
...

4 myA = ptr_createA();
printf ("ptr_createA gave me %p\n", myA);
myA->fun ();


If some one can explain in detail
closed account (LNboLyTq)
Hi TechEnthusiast,
Should you use code tags your code becomes more readable.
http://www.cplusplus.com/articles/jEywvCM9/

Andy.
typedef
http://en.cppreference.com/w/cpp/language/typedef

Yours say that word fun_createA is a typename. The type represents any function that takes no arguments and returns a pointer to type A object.

For example, these functions have that type:
1
2
A * foo();
A * bar();


Have you ever seen something like int * gaz; ?
The gaz is the name of a variable. The variable stores a pointer to an integer.

In your case: fun_createA * gaz;
The gaz is the name of a variable. The variable stores a pointer to a "function that takes no arguments and returns a pointer to type A object".

In your case: A * gaz;
The gaz is the name of a variable. The variable stores a pointer to a type A object.
Topic archived. No new replies allowed.