logo
down
shadow

How can I access a global pointer outside of a C function?


How can I access a global pointer outside of a C function?

Content Index :

How can I access a global pointer outside of a C function?
Tag : c , By : user135518
Date : January 10 2021, 01:47 PM


Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Access global pointer in another class


Tag : cpp , By : TBG
Date : March 29 2020, 07:55 AM
should help you out The compiler needs to know that p_list is declared elsewhere. Put the following in class1.h or in class2.cpp (at file scope).
extern type *p_list;

create value in function, then save pointer on value in global pointer array


Tag : cpp , By : simonth
Date : March 29 2020, 07:55 AM
Hope this helps 'test2' was not declared in this scope It's because the compiler doesn't know what test2 is. You need to add a function prototype above the main.
void test (int b);
void test2(int& j);
void test (int);
void test2(int&);

C++ member function pointer to global function pointer


Tag : cpp , By : walkah
Date : March 29 2020, 07:55 AM
it fixes the issue You can use a freestanding function that calls a wrapper which binds your instance to it. Here is rough example
#include <iostream>
#include <string>
#include <functional>

// global function which works
std::function<void(float, float)> memberCb;
void task_global(float x, float y) { memberCb(x, y); }
typedef void(*f_pointer)(float, float);


// try of using a member function
class BaseTask {
public:
    virtual void  task(float x, float y) = 0;
};


class ListeningTask :public BaseTask {
public:
    void  task(float x, float y) { printf("Called this member function with: %f %f\n", x, y); }
};

typedef void (BaseTask::*member_f_pointer)(float, float);

void callbackWrapper(BaseTask* t, float x, float y) { t->task(x, y); }

// the dll to use
class RegisterTask {
public:
    // no posibility to access or modify!
    void  subscribe(f_pointer fp) { 
        fp(1.0f, 2.0f); 
    }
    // just for demonstration how to use a member function pointer
    void  subscribeMemberDemo(member_f_pointer mfp) {  /*???*/ };
};



int main() {

    RegisterTask register_task{};

    ListeningTask listening_task{};
    memberCb = std::bind(&callbackWrapper, &listening_task, std::placeholders::_1, std::placeholders::_2);
    register_task.subscribe(task_global);

    return 0;
}
void callbackWrapper(const std::list<BaseTask*> &taskList, float x, float y) {
    for (auto t : taskList)
        t->task(x, y); 
}

int main() {

    RegisterTask register_task{};

    std::list<BaseTask*> taskList;
    for (int i = 0; i < 4; ++i)
        taskList.push_back(new ListeningTask);
    memberCb = std::bind(&callbackWrapper, taskList, std::placeholders::_1, std::placeholders::_2);
    register_task.subscribe(task_global);

    return 0;
}
#include <iostream>
#include <string>
#include <functional>
#include <list>


/* Simulated DLL */
typedef void(*f_pointer)(float, float);
class RegisterTask {
public:
    void  subscribe(f_pointer fp) {
        fp(1.0f, 2.0f);
    }
};



/* Static function generator to ease the pain to define all of them manually */
template<unsigned int T>
std::function<void(float, float)> &getWrapper() {
    static std::function<void(float, float)> fnc;
    return fnc;
}

/* Same here */
template<unsigned int T>
void task_global(float x, float y) { getWrapper<T>()(x, y); }



class BaseTask {
public:
    virtual void  task(float x, float y) = 0;
};
class ListeningTask :public BaseTask {
public:
    ListeningTask(int taskNum) : m_taskNum(taskNum) {}
    void  task(float x, float y) { printf("Called this member of task %d function with: %f %f\n", getTaskNum(), x, y); }
    int getTaskNum() const { return m_taskNum; }
private:
    int m_taskNum;
};


/* Context injector */
void callbackWrapper(BaseTask* t, float x, float y) {
    t->task(x, y);
}

/* Convenience function to bind an instance to a task */
template<unsigned int T>
void bindTask(ListeningTask* t) {
    getWrapper<T>() = std::bind(&callbackWrapper, t, std::placeholders::_1, std::placeholders::_2);
}

int main() {

    RegisterTask register_task{};

    auto task0 = new ListeningTask(1337);
    auto task1 = new ListeningTask(1984);
    auto task2 = new ListeningTask(42);

    bindTask<0>(task0);
    register_task.subscribe(task_global<0>);

    bindTask<1>(task1);
    register_task.subscribe(task_global<1>);

    bindTask<2>(task2);
    register_task.subscribe(task_global<2>);

    return 0;
}

How to use pointer to member function when when pointer to global function is required?


Tag : cpp , By : Brandon
Date : March 29 2020, 07:55 AM

Differences between global and pointer access


Tag : c , By : somebody
Date : March 29 2020, 07:55 AM
To fix the issue you can do Wondering what the differences between the following two bits (labeled Method 1, and Method 2) are, any benefits / risks associated with either, and finally, is there a better way. , Method 3:
void SomeFunction(int myInt, s_Thing* pWho)
{
    pWho->iAnInteger = 0;
}
Related Posts Related QUESTIONS :
  • Searching an element in an Array using Recursive Function in C Language
  • Most fastest C code to count recursively directories in Linux ( without files )
  • Why does C not offer syntactically transparent references like C++ and Java do?
  • Code doesn't get excuted after using continue in while loop
  • How can I maintain correlation between structure definitions and their construction / destruction code?
  • Avoid race conditions when using pointers and threads
  • Binary and Decimal converting
  • How to create input tensors and use with interpreter in Tensorflow Lite (experimental C API)?
  • Unexpected typecasting between values in C
  • Trouble with Forking Process and Calling bc using execve
  • Glib Threads vs GMain Loop Eventing
  • Why does the byte sequence turn when I cast a char array to an integer array?
  • Is there any difference usage in external interrupt between GPIO (AHB bus)and those (APB bus)?
  • Trouble programming AVR to interpret input from Arduino rotary encoder module
  • Is it correct to use a do-while loop inside a for loop? Why and why not?
  • Why we can't use dot for new created pointers to structs
  • Atomicity of fprintf from MPI processes
  • Printing of negative value in c via printf
  • What's the difference between global or local variables regarding the main function?
  • movsd from memory to xmm0 in c x86-64 jit
  • Problem with a function that insert the content of a csv into an array of struct
  • Segmentation Fault running time on sem_post(flag)
  • Extracting values from an incoming bluetooth serial on an arduino
  • "How much memory space does an array takes if the maximum size that is declared is not used?"
  • C GTK2 frustrated with gnome documentation
  • What really happens when a dynamic memory allocation is explicitly converted to struct type?
  • Re-Indexing Bits Within a Char
  • pointer de-referencing balagurusamy
  • Is it safe to memcpy regex_t?
  • Find a tight upper bound on complexity of the below program:
  • FFTW results differ from FFT in MATLAB
  • How to fix Misra 2012 violation , " Assignment operation in expression "
  • What is the difference between "int *p =0;" and "int *p; *p=0;"
  • Strncpy gives unwanted characters at end of string
  • What is really happening behind when a constant is assigned to a pointer variable?
  • Having trouble tracking logic of program includes Fork()
  • Are leftshift operators dependent on register size?
  • How to pass and receive back pointer to array of structures?
  • How to convert 2 bytes into a signed short in C
  • Using while loop to print Ascii Table with 10 characters per line
  • Is it possible to compress binary files with Huffman-encoding?
  • Why I am getting missing terminating " character?
  • Function call problem in data structure learning
  • Getting a segmentation fault trying to pass a node
  • Copying chars from file into an array
  • How to measure the time in seconds between two characters while user inserting them as an input
  • How to print two strings on the same line in c
  • Passing ("text"+1) argument to Strlen function in C. Why is Output 3?
  • to find the total no of letter in a string. please checkout my code too
  • Why do hiredis functions use void* instead of redisReply*?
  • Is there any case where the C static keyword should or could be used in header files for variables?
  • Sort an array in the relative order of elements of another array in c
  • Why functional programming over c
  • Re-assign value to variable if rand() repeats a number
  • does decrementing a NULL pointer lead to undefined behavior?
  • Access global variable value in different .c files present in different paths
  • Voltage Measurement on STM32
  • Printf behaviour with $ notation when sequence of arguments incomplete
  • Using "extern" to declare functions with no parameters
  • Stop mex function (C) without closing MATLAB
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com