C++: How to call a member function pointer that is a member of the same class?

featherbrain

I'm trying to implement more flexibility in my numerics by allowing me to choose different forms of a mathematical function and vary their parameters through instantiating them as objects of a certain class. That class includes certain mathematical functions I may choose plus parameters that I can vary. The constructor of the class sets a member function pointer in the class to a member function according to what mathematical function I want. I want to solely use the pointer to call whatever function it points to by directly using the pointer in my routine.

However, that proved daunting as I didn't know that member function pointers require a certain syntax and seem to work somewhat differently from regular function pointers according to what I could gather. I've experimented quite a bit and constructed myself a minimal example shared below.

#include<iostream>
#include<string.h>
#include<cstdlib>
#include<stdio.h>

class Someclass
{
public:
    // constructor to set pointer
    Someclass(std::string);

    // member function pointer to hold functions
    void (Someclass::*fptr)();

    // auxiliary function to call testfunction via pointer
    void call ();

    // testfunction
    void foo();
};

// testfunction
void Someclass::foo()
{
    printf("foo says hi! \n");
}

// call via specific function
void Someclass::call()
{
    (this->*fptr)();
}

// constructor
Someclass::Someclass(std::string name)
{
    if(name=="foo")
    {
        this->fptr = &Someclass::foo;
    }
}


int main()
{
    Someclass someobject("foo");

    someobject.foo(); // direct testfunction call: Works OK
    someobject.call(); // call via auxiliary function: Works OK
    //(someobject.*fptr)(); // direct pointer dereferencing: Gives Error

    return(EXIT_SUCCESS);
}

It shows that I can access the pointer by use of another member function that just calls whatever the pointer points to via use of a this pointer. However, I still can't seem to get the function call to work if I try to use the pointer directly in my main function through the line,

(someobject.*fptr)()

This particular expression leads to my compiler complaining about the scope and if I include the class scope, the compiler mentions invalid use of non-static members. Still, I'm confused as to why my implementation here doesn't work and if it does, how the proper syntax in my problem would be and why that has to be so.

Any insights would be really appreciated.

Captain Obvlious

The reasons for this is .* indicates a pointer to member function and does not directly reference the members of an object like the . and .-> operators. Since fptr is a member of Someclass and not a local variable you need to reference it directly like so

(someobject.*someobject.fptr)();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I call a C++ Class member function with the correct 'this' pointer like a normal C function? (pointer to class member function)

From Dev

How to call function with same name as class member

From Dev

How to call pointer member function inside a class definition?

From Dev

Call function through pointer to class member

From Dev

pointer to a member function of a class

From Dev

Pointer to class member function

From Dev

How to get a general function pointer as a private member of a C++ class?

From Dev

C++ How to pass member function pointer to another class?

From Dev

How to get a general function pointer as a private member of a C++ class?

From Dev

Want a static member function to call a member variable of the same class

From Dev

C++ function pointer to class member

From Dev

how to pass pointer to member function of a template class?

From Dev

Function pointer to class member function

From Dev

call member function in another class c++

From Dev

call member function in another class c++

From Dev

How to call class member function in javascript

From Dev

How to call function from a member class?

From Dev

Trying to use * pointer operator to call class member function

From Dev

Function pointer to class template member

From Dev

Return a pointer to class member function

From Dev

Member function pointer of template class

From Dev

Member function pointer on derived class

From Dev

Cast one pointer-to-member-function to another of same class

From Dev

cast a pointer to member function in derived class to a pointer to abstract member function

From Dev

cast a pointer to member function in derived class to a pointer to abstract member function

From Dev

C++: call pure virtual function from member function of same class

From Dev

C++ Pointer to a member function of any class with matching function signatures

From Dev

C++ function pointer (class member) to non-static member function of a template class

From Dev

Call a member function of other Class

Related Related

  1. 1

    How do I call a C++ Class member function with the correct 'this' pointer like a normal C function? (pointer to class member function)

  2. 2

    How to call function with same name as class member

  3. 3

    How to call pointer member function inside a class definition?

  4. 4

    Call function through pointer to class member

  5. 5

    pointer to a member function of a class

  6. 6

    Pointer to class member function

  7. 7

    How to get a general function pointer as a private member of a C++ class?

  8. 8

    C++ How to pass member function pointer to another class?

  9. 9

    How to get a general function pointer as a private member of a C++ class?

  10. 10

    Want a static member function to call a member variable of the same class

  11. 11

    C++ function pointer to class member

  12. 12

    how to pass pointer to member function of a template class?

  13. 13

    Function pointer to class member function

  14. 14

    call member function in another class c++

  15. 15

    call member function in another class c++

  16. 16

    How to call class member function in javascript

  17. 17

    How to call function from a member class?

  18. 18

    Trying to use * pointer operator to call class member function

  19. 19

    Function pointer to class template member

  20. 20

    Return a pointer to class member function

  21. 21

    Member function pointer of template class

  22. 22

    Member function pointer on derived class

  23. 23

    Cast one pointer-to-member-function to another of same class

  24. 24

    cast a pointer to member function in derived class to a pointer to abstract member function

  25. 25

    cast a pointer to member function in derived class to a pointer to abstract member function

  26. 26

    C++: call pure virtual function from member function of same class

  27. 27

    C++ Pointer to a member function of any class with matching function signatures

  28. 28

    C++ function pointer (class member) to non-static member function of a template class

  29. 29

    Call a member function of other Class

HotTag

Archive