How to Pass a Function Pointer That Points to Constructor

How to pass a function pointer that points to constructor?

You cannot take the address of a constructor (C++98 Standard 12.1/12 Constructors - "12.1-12 Constructors - "The address of a constructor shall not be taken.")

Your best bet is to have a factory function/method that creates the Object and pass the address of the factory:

class Object;

class Class{
public:
Class(const std::string &n, Object *(*c)()) : name(n), create(c) {};
protected:
std::string name; // Name for subclass
Object *(*create)(); // Pointer to creation function for subclass
};

class Object {};

Object* ObjectFactory()
{
return new Object;
}

int main(int argc, char**argv)
{
Class foo( "myFoo", ObjectFactory);

return 0;
}

Pass a function pointer as an argument between classes

This is a function pointer type to a free function (not a class member function):

typedef void(*myFunPtr)(void);

This is a B member function pointer type:

typedef void(B::*myFunPtr)(void);

Demo

Here's another Demo that will call a member function in a B. To do that, a pointer to a B and a pointer to a B member function is stored in A.

Passing a function pointer in to templated class

I think your issue lies in your initialization of myTree. I wrote some code that I think mimics your use-case. I believe the last line in particular will be the solution to your problem:

    //header file
template <typename T>
class TemplatedClass {
public:
TemplatedClass(int(*compFunction)(const T, const T)) :
funcCompare(compFunction)
{}
private:
int (*funcCompare)(const T i, const T j);
};
/////////////////////////////////////////////////////////////
//compare function
int compare(const int data, const int nodeData)
{
int returnValue;
if (data < nodeData)
{
returnValue = -1;
}
else if (data > nodeData)
{
returnValue = 1;
}
else
{
returnValue = 0;
}
return(returnValue);
}
//////////////////////////////////////////////////////////////
//initialization
TemplatedClass<int> tc(compare);

Hope this helps. Please let me know if I misunderstood something about your question.

Passing public member function pointer to a constructor

Suppose A has a member function named foo (matching the signature of typedef mem), then you can

A a(3, &A::foo); // pass member function pointer pointing to A::foo
(a.*(a.m))(); // call from the member function pointer on object a

LIVE

EDIT

If you want the caller to provide the implementation liek lambda, you can use std::function instead.

class A {
typedef std::function<int()> mem;

public:
A(int aa) : a(aa) {};
A(int aa, mem mm) : m(mm), a(aa) {}; // How to use this?

mem m;

private:
int a;
};

then

A a(3, [] { std::cout << "hello"; return 0; });
(a.m)();

LIVE

How to call a function from a function pointer of an instantiated object in C++?

You need to both get the SetInput member value from the IElement object, using a normal member operator, and then call the member function on the IElement object, using ->*. Assuming you want to use the same IElement for both:

(nodes[i]->*(nodes[i]->SetInput))();

Or perhaps rewrite the couple of statements as:

Elements::IElement* node = Elements::GetElement(i);
nodes[i] = node;
(node->*(node->SetInput))();

By the way, &(this->SetInput_Sum) is not an officially valid way to get a pointer to member. If your compiler accepts it, it's allowing it as an extension. The IElement constructor should be written:

IElement::IElement(bool normalizeInput)
{
if (normalizeInput)
{
this->SetInput= &IElement::SetInput_Sum;
}
else
{
this->SetInput= &IElement::SetInput_Const;
}
}

Passing this to a function from within a constructor?

When you instantiate an object in C++, the code in the constructor is the last thing executed. All other initialization, including superclass initialization, superclass constructor execution, and memory allocation happens beforehand. The code in the constructor is really just to perform additional initialization once the object is constructed. So it is perfectly valid to use a "this" pointer in a class' constructor and assume that it points to a completely constructed object.

Of course, you still need to beware of uninitialized member variables, if you haven't already initialized them in your constructor code.

Calling C++ member functions via a function pointer

Read this for detail :

// 1 define a function pointer and initialize to NULL

int (TMyClass::*pt2ConstMember)(float, char, char) const = NULL;

// C++

class TMyClass
{
public:
int DoIt(float a, char b, char c){ cout << "TMyClass::DoIt"<< endl; return a+b+c;};
int DoMore(float a, char b, char c) const
{ cout << "TMyClass::DoMore" << endl; return a-b+c; };

/* more of TMyClass */
};
pt2ConstMember = &TMyClass::DoIt; // note: <pt2Member> may also legally point to &DoMore

// Calling Function using Function Pointer

(*this.*pt2ConstMember)(12, 'a', 'b');

passing function as constructor parameter to base class constructor while initializing

Your constructor for bar is wrong.

The constructor bar(void (*getNextValue)(void)) does not expect a member function pointer of a function of foo and therefore initializing bar::memberFunctionPointer is not possible with the type of getNextValue.

You need to change the parameter in the constructor to void (foo::*getNextValue)(void) for this to compile.

Yet the overall design does not really seem right to me... So I think the answer of @wasthishelpful is more helpful ;-)

C++ How to make function pointer to class method

A member function is quite a bit different from an ordinary function, so when you want to point to a member function you need a pointer-to-member-function, not a mere pointer-to-function. The syntax for a pointer-to-member-function includes the class that the member function is a member of:

void (Game::*mptr)();

This defines a pointer-to-member-function named mptr that holds a pointer to a member function of the class Games that takes no arguments and returns nothing. Contrast that with an ordinary function pointer:

void (*ptr)();

This defined a pointer-to-function named ptr that holds a pointer to a function that takes no arguments and returns nothing.



Related Topics



Leave a reply



Submit