Hello there. Finally decided to make an account here :)

Anyways, I just started trying to implement function pointers in my code, and I seem to be having a few issues. (this is, of course, test code):

#include <iostream>

using namespace std;

class MyClass;

class SomeClass
{
public:
	SomeClass();
	MyClass* test;
	int SomeFunc(int x, char* y);
};

class MyClass
{
public:
	int (SomeClass::*funcptr)(int, char*);
};

int SomeClass::SomeFunc(int x, char* y)
{
	cout << "int x: " << x << endl;
	cout << "char* y: " << y << endl;
	return 0;
}

int main(int argc, char* argv[])
{
	SomeClass* someclass = new SomeClass();

	someclass->test->funcptr = &SomeClass::SomeFunc;
	(someclass->test->*funcptr)(2, "Lol"); // C2065: 'funcptr': undeclared identifier

	return 0;
}

Something tells me that this is functionality that I cannot do? And if so, I seem to be doing it wrong.

Basically, I have two classes: SomeClass and MyClass. MyClass right now just has a function pointer. SomeClass has a member function that I want MyClass to point to. However, trying to call that function seems to not be working the way I wanted it to. In fact, it's not working.

Any suggestions?

Recommended Answers

All 9 Replies

> (someclass->test->*funcptr)(2, "Lol"); Remove the * (someclass->test->funcptr)(2, "Lol");

> (someclass->test->*funcptr)(2, "Lol"); Remove the * (someclass->test->funcptr)(2, "Lol");

Thank you for your reply.

However, upon removal of of the *, I get another error on that same line: error C2064: term does not evaluate to a function taking 2 arguments This is all being compiled in Visual Studio 2008 Pro, by the way.

In g++, the error is: error: must use .* or ->* to call pointer-to-member function in `someclass->SomeClass::test->MyClass::funcptr (...)'

Are you trying to declare a SomeClass member from inside of MyClass?

Are you trying to declare a SomeClass member from inside of MyClass?

What I wanted was for MyClass to create a function pointer to SomeClass::SomeFunc(int x, char* y) .

You never declare 'funcptr', you just have it randomly appearing in main() referencing something. It also can't be declared in main() and then used as a type in your outside defined class.

You never declare 'funcptr', you just have it randomly appearing in main() referencing something. It also can't be declared in main() and then used as a type in your outside defined class.

18. int (SomeClass::*funcptr)(int, char*); From what I've read, that is how you declare a function pointer. A function pointer to a member function in SomeClass named funcptr that has a return value of type int with 2 parameters of type int and char*.

Then, to define what that function pointer actually points to:

32. someclass->test->funcptr = &SomeClass::SomeFunc; But my assumptions must be wrong and I'm doing something wrong somewhere.

SomeClass::*funcptr doesn't exist, its not a SomeClass member. I believe it would be something more like:

typedef int (*fp)(int, char*);
class MyClass{
public:
    fp *funcptr;
}

I cannot confirm that is correct, I need to oil up the rusting gears in my head. I generally don't use pointer functions unless I'm using a raw code catalyst of sorts. I'm sure someone will sort this out.

I think this is what you were after, note that you need an instance of the class through which you make the call ( pB = new B; below)

#include <iostream>
struct B;
struct A
{
  B * pB;

  A();
  ~A();

  void func(int i, const char * p) 
  {
    std::cout << i << std::endl << p << std::endl;
  }
};

struct B
{
  void (A::* funcptr)(int, const char *);
};

A::A()
{
  // a B is needed
  pB = new B;
}

A::~A()
{
  delete pB;
}

int main()
{
  A * pA = new A;

  pA->pB->funcptr = &A::func;

  (pA->*(pA->pB->funcptr))(2, "Lol");

  delete pA;

  return 0;
}

I think this is what you were after, note that you need an instance of the class through which you make the call ( pB = new B; below)

#include <iostream>
struct B;
struct A
{
  B * pB;

  A();
  ~A();

  void func(int i, const char * p) 
  {
    std::cout << i << std::endl << p << std::endl;
  }
};

struct B
{
  void (A::* funcptr)(int, const char *);
};

A::A()
{
  // a B is needed
  pB = new B;
}

A::~A()
{
  delete pB;
}

int main()
{
  A * pA = new A;

  pA->pB->funcptr = &A::func;

  (pA->*(pA->pB->funcptr))(2, "Lol");

  delete pA;

  return 0;
}

That was just what I was looking for. Although, line 38 threw me a bit off, I must admit. I had to read that a few times before I got it straight, the second pA seemed almost redundant, but now I get it. Thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.